I recently finished a book with one of those trick endings. This one was a SFnal take on the "it was all a dream" line of BS. Well, let me clarify. The "it was all a dream" ending isn't always crap and it isn't always a trick, either. But those stories are definitely the exception rather than the rule. Why is this ending so hard to do well? Mostly because of the contract with the reader.
What is this contract? It's not really signed or verbal. It's more of a gentleman's agreement, if you will. An understanding--well, for commercial fiction, at least. When a reader picks up a book and starts reading, she does so with the expectation that if there's a mystery, she will be able to solve it as the hero does (or do a "duh, of course that's whodunnit, why didn't I see that" facepalm at the reveal). The problem that is presented in the first act will be mostly resolved in either a positive or negative fashion by the last act. In short, she should be able to find and follow narrative threads through to their conclusion. That's the deal.
When the author pulls the "it was all a dream" fast one without the appropriate laying down of that narrative thread (or drowns it in other far more plausible threads that never get resolved), the contract is broken. The threads the reader was juggling are severed or vanished all together. She sits back and looks at the book and wonders, "What the hell did I miss? Why was I even bothering to care about problem X?" And then the picking apart of the story logic starts, the hunt for those missing clues and explanations that maybe, just maybe might have provided for the reading she obviously didn't get the first time around.
This is not a good place to put your reader. It is not sporting to write a book that seems like it was designed to make the reader feel stupid.
To be fair, the author doesn't have total control over the contract with the reader and maybe deserves some exculpation. Cover art and copy are two things that can very easily con a reader into thinking she's reading one thing when she's actually reading something else entirely.
In the last three books I've read where the ending revealed that the story was something completely different than what I thought it was, the culprit in one case was the cover copy alone. In the other two, the cover copy certainly started me in the wrong direction, but the authors didn't do anything to disabuse me of that misunderstanding until the end. In one of those two cases, the established universe practically begged for the reading I gave it, but the author spun a whodunnit tale that dismissed the foundation of my reading as something merely coincidental. In the book I just finished, the author fed me three fascinating storylines that were just about to head into the fourth act when the trick ending appeared.
In the latter two books, better cover copy would have helped, but clues as to what I should be reading in the first chapter would have made all the difference.