A foil is a character who acts as the opposite to another character and consequently highlights important features of that character's personality. Some examples in literature include Harry Potter and Draco Malfoy, X-Men's Professor X and Magneto, and Doctor Jekyll and Mr. Hyde.
The use of foil characters helps a writer demonstrate the relative importance of different character traits in similar situations and therefore contributes to the theme, or lesson, of the story. For example, in the Harry Potter books, Harry is a humble, trod-upon, orphan from a non-wizarding background. He is contrasted with Draco Malfoy, an arrogant, pure-blood son of a wealthy and respected wizarding family. Their differing responses to the pressures they face helps the author show the reader the better way to behave. Without a foil, those qualities potentially go unnoticed by the reader.