In some ways the end is the most important part of your story. It can also be the hardest part to write.
Here's a simple secret that can make your story's ending both unexpected and satisfactory. It's not even that much of a secret because you've probably experienced it yourself:
"You can't have what you want the way you want it."
Suppose you're writing a story about a guy who wants to win the Iditarod dog sled race. Not just run it--win it. You've started out well and put in lots of excitement and tension with a blizzard, a sinister protagonist, and a sled dog with a Jekyll/Hyde personality who can unpredictably turn vicious. Now you're almost to the end; maybe the finish line is even in sight.
If you're anything like me, here is where you suddenly get stuck. If you simply make the hero win, the audience will be bored or even feel let down. From the start everyone expected the hero to win and nobody likes an end he can see a mile off. On the other hand, if the hero doesn't win, what was the point of the story?
Here is where you use the simple principle above.
In the first place, what the hero wants on the surface is not usually what the hero really wants. Your hero doesn't just want to win the race, perhaps he wants to prove himself--to himself or to someone important to him--or maybe he needs the prize money to send himself to college or for cancer treatment for his sick grandmother.
But let's say that he gets his surface desire--he gets to win the race. Here's what could happen:
The hero (let's call him James) is on the last day of the race. Suddenly Dr. Jekyll turns into Hyde and lashes out at several of the other dogs, causing a fight and injuring James when he tries to break it up. Now James is not only behind, he's also handicapped. He's not even sure he can make it to the finish line.
Just when all seems lost the Jekyll-Hyde dog turns Hyde again, but this time to good purpose. It pulls the rest of the team and the injured James from eleventh place all the way to first, passing the leading sled team just as they cross the finish line. James gets to win the race like he wanted to, but not in the way he expected to. He thought he had what it took to win the race, but in the end he found he couldn't win it on his own.
Even though it isn't what he expected, this ending might be better for James than winning the race on his own because it gives him a better relationship with his dog and teaches him that you can be an asset even when others write you off as a liability.
Let's try the principle the other way--things go the way James expects but he doesn't get what he wants. You could do this simply by making him get to the end of the race and making someone else pass him at the finish line, but the story would be pretty boring up to that point and very unsatisfying at the end. Don't do that to your readers just to create a surprise ending.
Instead, you might make James fall behind when he gets lost in the blizzard. He realizes that he can't catch up in time, but there's a short-cut through a small canyon that only James knows about. He cuts through it and wins the race at the last minute. Even though things go as he planned and he gets what he wanted on the surface, he doesn't get what he really wants. He knows he didn't win the race fair and square. The only thing he's proved to himself is that he's a cheater.
Even though it's a sad ending and not very satisfying, it's still a strong ending. It shows the readers the truth about James, winning races, and life in general. Modernist literature often adopts the second part of the principle. The characters do what they feel like doing, think they're having fun, and then wind up at the end realizing that they never did truly enjoy themselves. You could also write a story about a guy who wanted to be rich, got rich, and then found out it wasn't really so great. Maybe what he really wanted was for people to like him but what he did to get rich made people hate him instead.
I said it was an either/or proposition, but there's actually a third option. The hero might not have things his way and he might still not get what it was he wanted.
For instance, James might be winning the race when suddenly the sinister antagonist shows up alongside him and broadsides James's home-made dogsled, smashing it to matchwood. In doing so, the bad guy comes too close to James's vicious lead dog and it attacks his team. James knows he can't win now but he can keep the other guy from winning. Instead, James decides to play fair and calls his dog off. He loses the race but he gains his dad's respect and approval. And maybe for James that's better than winning.
The first option made a happy ending, the second made a sad ending, but this third option makes a happy-sad, flip-floppy sort of ending. It's also a more mature ending because kids don't like it very much but adults do. Every ending has to have a bit of sadness in it from all the things the hero had to lose or give up in order to reach the point where he now is. Every one of these options can make your ending unexpected and keep your readers wanting to find out what will happen right up to the last page. They can also make your ending solid with a note of finality that leaves your readers feeling that that is exactly what should have happened.