This week I'm going to talk about a trope duality ; the Deus and Diabolus Ex Machina. These are Latin terms that mean "God in the Machine" and "Demon in the Machine" respectively. Both of them describe a situation where the author takes a desperate/hopeful situation for the heroes and introduces a wild element that twists it in the other direction. They are two sides of the same coin. Whether this device is used to achieve a happy ending or a sad ending is irrelevant; both of them are bad writing practices because they show the hand of the author and create unfulfilling endings.
We all know that the hand of the author writes the story but we don't want to see it. It is distracting and cheapens the characters because it reminds the reader that they are less than puppets. If the reader cannot become invested in the characters then they don't care about what happens to the characters (i.e. the plot). There are occasions where they can be used in service to the plot (played for laughs, a post-modernist premise, etc) but generally speaking, readers do not want to be reminded of the author when immersed in the author's world.
From another prospective, if the answer to a hypothetical question regarding the story is 'otherwise it would be too short/the author's point wouldn't be made' etc then the story is not compelling or developed enough to answer the question on its own. Again, this can work depending on the genre and premise but isn't it much more fun to imagine how the character will solve their problem/fail to solve their problem instead of how the author can favoritize/screw them over?
Both Machina lead to unfulfilled endings. On the Deus hand, all the struggles of the heroes and all the evil committed by the villains come to naught because some outside force decided to resolve everything itself. The reader is left feeling a sense of pointlessness and thinking that the author shoehorned a happy ending. On the Diabolus hand, the conflict of the story also comes to naught for the same reason but to a darker effect. The reader is left feeling that that the author is an asshole (to reader and/or the characters) or believes tragic endings are more 'artistic' or some nonsense. Whether good or bad, an ending should be the culmination of everything the story has been leading up to in order to create a sense of climax and resolution. If the ending you want is at odds with the story written thus far, then the story should be rewritten instead of adding a hasty ending. Your readers will call foul otherwise.
(For another post on the merits of happy and sad endings, see "Earn Your Happy Ending"
These kinds of ass pulls are snark bait on Tvtropes and other parts of the internet. This is why it's so important to keep these two tropes in mind when approaching the ending. Whether it is happy or sad or open-ended, your readers do not want to see it butchered by either machina.