Writing isn’t magic. You can’t just say “Presto! A rabbit!” and pull a rabbit out of a hat. (Well, you can, but nobody is likely to believe it.) No, if you’re going to pull a rabbit out of a hat, you have to start by sneaking the rabbit into the hat while nobody is supposed to be looking.
There are two ways of doing this: first, you know that at some point you’re going to need the rabbit in the hat, so you mention the rabbit in chapter one and the hat in chapter three and a few chapters later you comment about the rabbit’s interest in the hat and then you establish that the rabbit has a habit of crawling into small, dark places it’s not supposed to be. Then, when you need the rabbit to come out of the hat, nobody will think it’s an unbelievable coincidence.
The second way is to do all of that after the fact. That is, you discover while writing Chapter Twenty that you need a rabbit to come out of a hat right then, so that’s what you do. But it’s completely deus-ex-machina, out of the blue. It’s a really unbelievable coincidence. It doesn’t work. But you need it to happen that way; it’s right. So you go back to the first chapter or two and find a place where you can mention the hat, and a different place where you can mention the rabbit, and so on.
Oh, wait, there’s a third way: you get to the middle of Chapter Twenty and realize that you need something to happen right then, but you’re not sure what. So you look back over the first few chapters and realize that you mentioned a rabbit that would be just perfect for distracting the villain at the critical moment. And you check over the next few chapters and don’t find another mention of the rabbit, but there’s a mention of a hat. Then you do a search on “rabbit” and discover that the rabbit has been nibbling on the hat in Chapter Ten…it’s all there already, all you have to do is have the rabbit pop out of the hat.
Basically, what I’m talking about here is setup. Sometimes, it happens by accident, as in that “third way,” but you can’t depend on things going that well. More usually, either you know the event is coming and you build in the setup as you write, or you go back and fill in the bits that make the event believable after you write the scene where it happens.
Either way, what you’re looking for are ways of establishing various elements of the Big Surprise so that when it happens, it’s plausible. It’s not quite the same as foreshadowing, though it is related. For instance, in The Lord of the Rings, Isildur’s last-minute refusal to destroy the ring both foreshadows Frodo’s refusal and is part of the setup (in that it is one of many reiterations of the fact that it is practically impossible for anyone who has the ring to give it up voluntarily, let alone see it destroyed). Bilbo’s reluctance to give up the ring, on the other hand, is more setup than foreshadowing (I think); Gollum’s obsession with it is definitely setup.
The terminology is not actually particularly important for a writer; the important thing is that there is a difference between warning the reader that something big is about to happen and getting the reader to believe in whatever happens. The classic “dark clouds hovered ominously on the horizon” is usually foreshadowing trouble to come; it becomes setup when the characters are caught in a downpour a few hours later.
The one thing I have never seen work is trying to do all of the setup after the fact. Which is not to say it couldn’t ever work that way, but nearly all the writers I’ve seen try it have been beginners who either can’t figure out how to set up their Big Surprise, or can’t bring themselves to go back and put in the setup after they’ve written the scene.
In the first case, the problem is often that the author has chosen a single-viewpoint story, and the most obvious place for the setup is in a scene or scenes for which the viewpoint character wouldn’t be present. The solution is to look for non-obvious places and more indirect setup; instead of mentioning the rabbit, the hat, etc., the writer might have a character complain about rabbits hiding in ridiculous places, and later someone gets a letter about his niece receiving a rabbit for her birthday. A more complete explanation may still be needed after the fact, but it will be filling in the blank spots, not trying to create a whole canvas from scratch – it ends up being like the detective’s explanation in a mystery novel, stringing together the clues that were right there all along.
In the second case, the author is often terrified of giving too much away too soon. They are so focused on the necessity of the Big Surprise being a surprise that they are afraid to tell the reader anything, for fear of tipping them off. This writer needs to get over it and accept that for some exceptionally perceptive readers, the story is going to be a thriller, not a mystery – that is, the point is not finding out whodunnit, but watching the protagonist beat the antagonist to the finish line – and then go back and put in some setup. In extreme cases, the writer needs a reliable first-reader who can tell them that they need more setup and no, it won’t spoil the ending (or, very occasionally, that they really have done too much and need to obscure some of it if they want the surprise to be surprising).
Note that none of this applies to the author who is deliberately putting most of the plot offstage (John M. Ford comes instantly to mind). That’s a completely different style of writing, and whether one likes it or not is a matter of taste, not flaws in the construction.