A foil is a character that contrasts with another character, usually the protagonist, and so highlights various facets of the main character's personality. A foil usually has some important characteristics in common with the other character, such as, frequently, superficial traits or personal history. The author may use the foil to throw the character of the protagonist into sharper relief.
A foil's complementary role may be emphasized by physical contrasts. Dreamy and impractical Don Quixote is thin; realistic, practical Sancho Panza is fat. Sherlock Holmes is tall and lean; Doctor Watson, although at first, on his return from Afghanistan, described as lean, is later described as "middle-sized, strongly built."
In some cases, a subplot can be used as a foil to the main plot. This is especially true in the case of metafiction. One example of a plot being used as a foil can be seen in the graphic novel Watchmen, in which a comic book within the Watchmen universe presents a story similar to that of one of the main characters.
The "straight man" in a comedy duo is a comic foil. While the straight man portrays a reasonable and serious character, the other portrays a funny, dumb, or simply unorthodox one. The humor in these partnerships derives from the interactions between these drastically different personalities.
The term foil refers to the practice of putting polished foil underneath a gemstone to make it shine more brightly.
It is also likely that widespread use of the word "foil" in literature comes from the play Hamlet by Shakespeare, in which Hamlet says that "I'll be your foil, Laertes: in mine ignorance / Your skill shall, like a star i' the darkest night, Stick fiery off indeed" (Act 5 Scene 2).