There are several methods that I consider when using a surprise twist ending:
The Dream - The lazy writer's way out. The entire sequence of events was played out in the main characters mind. Usually cheats the reader because it's a long and tortuitous tale to get to where we are and nothing was learned. There are exceptions, but if this is someone's nightmare, the good ones are few and far between.
The Forshadowed Weird: This is a better "The Dream" if only because there are obvious signs that something strange is afoot. These will often take real life tells people can use to figure out if they are actually dreaming. For example, the lack of change in ambient lighting despite turning a light switch on OR really bad jump cuts (usually in film and tv) or a jumble of non-sense where symbolic notation (writing, numbers) should be... and an ability to still understand what the writing says despite this problem... or for male characters, a noticiable lack of female characters. These are all real life phenomeana of Dreaming (ambient light doesn't change, one is always in the next scene set with little recollection of the trip, dream states do not retain functional written language memories, and men tend to populate their dreams with just men where as women populate their dreams with both sexes) which is a rather weird process we don't really understand.
This doesn't need to be a Dream either. The lack of specific functionality (i.e. a facet won't turn on, despite moving the nobs) or bizarre functionality (the facet does turn on, but coffee comes out... and it's not good coffee either). Basically, its hard signaling to the audience that something is not right and allows them to solve the mystery along with the heroes. A lot of Twilight Zones ended with these kind of twists. A couple wakes up in a house with no working appliances (they are trapped in a giant alien's doll house and are now her toys... OR they are really toys brought to life in a toy box...)
Then you have what I like to call "Two Stories One Book"... this is basically the Sixth Sense where someone watching the movie for the very first time doesn't realize the clues are subtly placed and come back into play in the twist. Half the fun of these movies is a person watching it for the first time who is sitting next to a person who is seeing it for the second time... They are essentially watching the same scene but paying attention to different scenes. Here the twist need not be supernatural, but mundane... the only rule is that the audience forgets the elements that make it apparent when watched the second time. This can appear in things such as mysteries, legal drama (My Cousin Vinny, for example, needs to be re-watched several times to see all the short jokes that become vitally important), horror, and even comedy (Hot Fuzz is funny the first time though, but many of the jokes in the third act were well referenced through out the film... one of the best moments is the reveal of the killer... it's both not who you expect, and exactly who you would expect at the same time... the reasons for all the murders are also pretty funny as the hero suspects that its a complex real estate inheritance plot... but they're all much pettier reasons for the murders, often times mentioned in the same lines of dialog as the more sinister lines of dialog... and it's still a grand conspiracy).
Harry Potter also tried to pull this off, but J.K. Rowling's fans were wise to her doing this and suspected the trick. To her credit, one clue was the initials of the guy who did the thing... but and Rowling did provide two characters in the original English that had those initials... but she didn't preserve this in international translations, in which one of the character's consistently lined up with the initials in every translation, while the other character almost never matched... the result were fans were able to guess correctly one book before the reveal.