Not an easy skill to develop.
Perhaps you start with the inspiration for the ending, and there's the little question of thinking that you should consider whether to hide significant elements so it can surprise the reader. If you do manage to think of it, it can be really interesting trying to think yourself back into the mind of someone who doesn't know it -- it was the original inspiration after all, the first thing you knew -- to set it up as a surprise.
Or if you muddled about a bit and then happened on it, it has some advantage in surprising you, but there's no guarantee that you put in the hints and the foreshadowing and the rest of the architecture you need. Surprise is a thing of chiaroscuro: the time when all was dark, for the moment when a flash of light illuminates all. You need to build the darkness first.
sigh Hard to wrestle with.