In any narrative, a foil is a character who contrasts with another character; typically, a character who contrasts with the protagonist, in order to better highlight or differentiate certain qualities of the protagonist. A foil to the protagonist may also be the antagonist of the plot.
In some cases, a subplot can be used as a foil to the main plot. This is especially true in the case of metafiction and the "story within a story" motif.
A foil usually either differs dramatically or is an extreme comparison that is made to contrast a difference between two things. Thomas F. Gieryn places these uses of literary foils into three categories, which Tamara A. P. Metze explains as: those that emphasize the heightened contrast (this is different because ...), those that operate by exclusion (this is not X because...), and those that assign blame ("due to the slow decision-making procedures of government...").
The word foil comes from the old practice of backing gems with foil to make them shine more brightly.
Shakespeare refers directly to the origin of the term "foil" in Henry IV, Part 1. Prince Hal says that when he starts behaving better, the change will impress people:
And like bright metal on a sullen ground/ My reformation, glittering o'er my fault/ Shall show more goodly and attract more eyes/ Than that which hath no foil to set it off. 
In Emily Brontë's 1847 novel Wuthering Heights, Edgar Linton is described as opposite to main character Heathcliff, in looks, money, inheritance and morals, however similar in their love for Catherine.
In Frankenstein; or, The Modern Prometheus, an 1818 novel by Mary Shelley, the two main characters—Dr. Frankenstein and his "creature"—are literary foils to each other, functioning to compare one to the other.
In David Copperfield, an 1850 novel by Charles Dickens, Edward Murdstone's marriage to David's mother Clara, contrasts with David's future marriage to Dora Spenlow, presented with a different outcome if David had endeavored to subdue his wife's caprices, as did Edward Murdstone with Clara's.
In Jane Austen's 1813 novel Pride and Prejudice, Mary's absorption in her studies places her as a foil to her sister Lydia Bennet's lively and distracted nature.
Similarly, in William Shakespeare's tragedy Julius Caesar, the character Brutus has foils in the two characters Cassius and Mark Antony. In the play Romeo and Juliet, Romeo and Mercutio serve as character foils for one another, as well as Macbeth and Banquo in the play Macbeth. In the tragedy Hamlet, a foil is created between Laertes and Prince Hamlet to elaborate the differences between the two men. In Act V Scene 2, Prince Hamlet tells Laertes that he will fence with him and states, "I'll be your foil, Laertes" (5.2.272). This word play reveals the foil between Hamlet and Laertes that was developed throughout the play.
George and Lennie are foils to each other in John Steinbeck's 1937 novella Of Mice and Men. Lennie is huge and strong as a bull but mentally slow, while George is small, skinny and very smart.
In the Harry Potter series, Draco Malfoy can be seen as a foil to the Harry Potter character; Professor Snape enables both characters "to experience the essential adventures of self-determination" but they make different choices; Harry chooses to oppose Lord Voldemort and the Death Eaters wholeheartedly, whereas Draco struggles with his allegiances through the whole series.
((cite book)): CS1 maint: others (link)