The ending of the last of us sees two different moralities playing out. On one side is a typical Utilitarian perspective (i.e. actions are moral if they create good). The Firefly's see the death of Ellie as moral because of the good that will be achieved because of it. On the other side of this is a Kantian perspective. Actions are not moral if you're using another person as a means to an end. Joel views the Firefly's as using Ellie as a means to an end without her permission, and he sees this as a moral action that must be corrected.
Note that in order for this to be an immoral action, Ellie is never asked, in the hospital, about her wishes. If she is told and she consents to the procedure, which I suspect she would have, then it becomes a moral action, as she would no longer be used as a means to an end, but she becomes the end itself. You can't 'use' another person if they consent to your actions.
This situation contrasts with another theme of the game where it's considered 'right' to kill another if it means assuring your own survival, as everyone is trying to do the same thing. This kind of statement is a morally ambiguous, because no real good is created (from a Utilitarian perspective), and it's not moral from a Kantian perspective, because you're using someone to further your own ends. So, in other words, Joel sees it as ok to kill another to assure his own survival, but he does not see it as moral for the Firefly's to kill Ellie to ensure humanities survival.
Of course, at the very end of the game, by lying to Ellie about the nature of the Firefly's, he acts immorally, in order to keep Ellie happy to stay a part of Joel's life.