Foreshadowing is used in many different ways, in many different stories. It is especially obvious in prequels, such as C.S. Lewis' The Magician's Nephew, a backstory for his The Lion, the Witch, and the Wardrobe which tells Professor Kirke's story and that of the birth of Narnia. The whole story could be referred to as a foreshadowing, really, as it sets the stage the arrival of the Pevensies and all that follows thereafter, but there are also specific elements that are more noticeable; for instance, the poignant scene in which Digory Kirke encounters Jadis at the gate of the Garden. On the gate inscribed in silver are the words,
"Come in by the gold gates or not at all,
take of my fruit for others or forbear,
for those who steal or those who climb my wall
shall find their heart's desire and find despair."
Naturally, Jadis climbed the wall and ate one of the apples of youth, establishing herself as a permanent evil in Narnia, and that looks forward to the time when she rules Narnia as the White Witch. Not only does The Magician's Nephew answer questions that arise in the other books of the series, but it adds depth to the world Lewis created and to the events in The Lion, the Witch, and the Wardrobe. This is the same for any series in which the author ties the books together, especially those where the plots are only loosely bound together, yet have similar threads running through them. In Rosemary Sutcliff's Eagle of the Ninth and its sequels, she links the novels together with the use of a family ring that passes down the generations of the Aquila family. (While this is not strictly foreshadowing, the principle is the same.) On the other hand, a series in which foreshadowing is not used would be Madeleine L'Engle's Time Quintet, in which the second book makes no mention of the events in the first and thus lacks continuity.
But one problem with this is that writers don't always know where they are heading with a story; after all, Tolkien had only fragments of ideas for The Lord of the Rings when he began The Fellowship of the Ring. When this is the case, it frequently shows in rough drafts as the writer's knowledge of the story increases. Thus comes the need for editing, and it is in this process that foreshadowing can be added as the writer weaves the two halves of the novel more tightly together (something that Jenny wrote an excellent post on a little while ago).
Another use for foreshadowing is to keep readers turning pages. It is not always the best option to drop the reader in the midst of an action scene on page one; sometimes a more "normal" setting is needed to paint a background for the rest of the novel and set up the plot, and so the beginning of a story often has a slower pace than the middle and climax. In such cases, hinting in the first chapters about what is to come later helps keep readers tantalized and waiting for the next twist in the plot.