I disagree with Shamus's view on the ending. I feel it was clearly intended to be morally grey and ambiguous, at least in part, and I think it succeed. So while I agree with Shamus that Joel is by no means clearly in the wrong, I also think he portrays the Fireflies as more cartoonish and, well, evil then they really are.
Granted, this is a fuzzy area that can only really be explored though speculation, since the fireflies aren't in the story that much and we only ever really see them from a (neutral/hostile) outsider's perspective, but as I recall that the game clearly shows that defeat has made the fireflies are desperate and angry. Over the last year or so they've watched their comrades die by the hundreds as the military has stated stamping them out (culminating with Marlene's defeated march from Boston), and the cure research has flatlined. Remember that the Fireflies think they're salvation of the human race--(since they seem to be the only organized group still searching for a cure, at least in the US, this conviction is not without basis.) With their defeat looming increasingly large, humanity seems doomed by extension. This fatalistic mindset is heavily implied, and while I think it could have been made more explicit I still feel that it amply explains and justifies, from a storytelling and character perspective, the anger and general lack of respect for Joel's wishes among the fireflies.
Now Shamus interprets the above-mentioned defeats as a sign of general incompetence, but I think it's more a sign of impotence. Being defeated through incompetence implies that the fireflies could have succeeded if they hadn't made bad decisions, but it seemed to me that the game presented them as being more outmatched from the start, and increasingly driven to acts of desperate cruelty as a result. Remember, they've been operating for years and in the early days won several victories; it's mentioned that they successfully liberated multiple cities from the military in years past, and were conducting promising vaccine trials at one point. I got the impression that it was only over the last few years or so that it became clear that the Fireflies couldn't win, that the challenges facing them were greater then their early victories implied. So I don't think it's fair to portray them as pure failures.
As for the CPR scene, the fireflies do have reason to view Joel as a threat. As someone else mentioned, they possibly behaved so callously to Joel because they were suspicious it was a trap (he is a trespasser on their secret base, after all). Also keep in mind that even when they realize who Joel is, they still see him as a low-life mercenary in it for the money--and indeed he was, when the Fireflies saw him last. And besides that, the simple fact that he's not a firefly makes him a threat--as far as they know, he might alert the military if they let him escape. So that might explain why they treat him as a threat even when he makes no hostile move.
Concerning the hasty decision to extract the brain, even if the previous cure research has failed, we know the fireflies have conducted many tests in the past, and these experiments have convinced them that they know exactly what they're doing when they decide to cut open Ellie. Even if this isn't true, I think the Firefles actions are again at least explicable.
In short, I think the Firefly's ruthlessness is a mirror of Joel's own, rather then just a symptom of sloppy writing that has no basis in character. Joel is convinced that Ellie is his last chance to save his own humanity, while the Fireflies think she's their last chance to have humanity, period. I feel that to argue that the fireflies are acting irrationally is to miss the point; their actions make sense from an emotional perspective, as does Joels, and I feel that that's what counts the most.