An ending does not have to be happy; it has to be satisfactory. That is, it has to affirm something that the reader believes, or wants to believe, about the world. That can be something sad. It is often something sad. Sad stuff happens and we have to deal with it. It is often preferable for people to believe something sad about the world than to believe that life is simply chaotic. Sadness I can understand is more tolerable than mere chaos that I cannot understand.
Stories are, for the most parts, assertions about orderliness in human life. They assert that the world makes sense. As so many people like to say, "everything happens for a reason". Outside of a specifically religious view of the world, or as an expression of pure determinism, it isn't clear what this claim really means, but it clearly comforts people greatly to believe it. Stories are an illustration and an affirmation of this faith that everything happens for a reason. This is why there are rules about what works and does not work in a story, why we can define a shape in stories and discover archetypal characters who play specific roles in stories: all of this is about everything happening for a reason.
There are, of course, stories that assert the opposite: that life really is chaotic and that, other than in the strict determinist sense, nothing happens for a reason. But these are not shaped like regular stories. They are not so much stories as anti-stories. And very few people read them.
Even hard materialists, who should logically affirm that the things that happen in human life are meaningless, nevertheless prefer regular stories, which, implicit in their very construction, assert that everything happens for a reason, because, in a story, everything does. I suppose (if they cared to) such folks could justify this choice by saying that reading stories that affirm the meaningfulness of life is psychologically comforting, and that such comfort is a useful thing in a universe where, in fact, nothing happens for a reason.
And that is really the point about stories. A good story is satisfying because it affirms our hope/need to believe that everything happens for a reason. This is perhaps even more needful when it comes to facing the tragedies of life. It is when tragedy strikes, seemingly out of nowhere, that people are most likely to say, "everything happens for a reason". Tragic literature is an affirmation of this belief.
When good things happen, it is less urgent to affirm that they happened for a reason, but when bad things happen, the idea that they happened for a reason may be our last bulwark against despair. Thus tragedy has as much of a role to play in our lives as comedy, if not a greater role.