Anton Chekhov, a Russian playwright, stated that "one must not put a loaded rifle on the stage if no one is thinking of firing it." In writing a story, an element introduced briefly at the beginning of the story may be used to foreshadow the events at the end. If the protagonist of your story notices a poster about a lost dog in the first few pages, for example, that small detail could foreshadow the lost dog playing an important role in the final scene.
Another way of foreshadowing involves the use of patterns throughout the work to point to events that will occur later in the story. This technique gives the audience a much stronger hint as the what will occur later in the story. For example, the repeated image of the train in "Anna Karenina," along with the train accident when Anna is first introduced and the character's increasing alienation from her surroundings, suggest to the reader that a train will play some part in her death.
A self-fulfilling prophecy is another method of foreshadowing, involving a character that predicts the outcome of the story. The protagonist then inadvertently fulfills that prophecy, often by trying to avoid it. A very early example of this technique is found in "Oedipus Rex," when the protagonist flees from home to avoid fulfilling a prophecy, only to make it come true. Shakespeare's "Macbeth" also contains a self-fulfilling prophecy, as Macbeth's belief in the witches' prophecy leads him on a path to his predicted death.
Many authors use foreshadowing in more subtle ways, through the repetition of themes and symbols. A flooded house early in a novel might be symbolic of a storm the character has to face later, or a repeated motif of snakes throughout the work may suggest the protagonist being betrayed late in the story. These symbols and themes can be as subtle or as overt as the author wishes them to be, depending on how much of a hint they want to provide to the reader.