In the YAsecrets chat tonight on Twitter, Holly Cupala mentioned that she didn't know one of her characters' big secrets until the end of her book (Tell Me a Secret). And when she went back to plant the seeds for that secret, she discovered most of them were already there. Other writers had also had this experience (including me!).
Writing involves several levels of our minds, not all of them conscious. If we immerse ourselves deeply enough in the story, those seeds and those connections often arise organically. Sometimes, that's the way we know we've stumbled upon the right ending or plot twist; we find that the whole book has in fact led to this moment, whether we understood it at the time or not.
Plenty of conscious planning goes into a book, too, of course. And I find I often use a hybrid approach: I'll notice that I've used a certain symbol or image a few times, and it happens to fit with my theme, so I start to play it up consciously during revision.