foil (v.) Look up foil at Dictionary.com
c.1300, foilen "to spoil a trace or scent by running over it," irregularly from Old French fouler "trample," from Vulgar Latin *fullare "to clean cloth" (by treading on it), from Latin fullo "one who cleans cloth, fuller," of unknown origin.

Hence, "to overthrow, defeat" (1540s). Sense of "frustrate the efforts of" first recorded 1560s. Related: Foiled; foiling. Foiled again! as a cry of defeat and dismay is from at least 1847.
foil (n.) Look up foil at Dictionary.com
"thin sheet of metal," early 14c., from Old French fueille "leaf," from Latin folia "leaves," plural (mistaken for fem. singular) of folium "leaf" (see folio).

The sense of "one who enhances another by contrast" (1580s) is from the practice of backing a gem with metal foil to make it shine more brilliantly. The meaning "light sword used in fencing" (1590s) could be from this sense, or from foil (v.). The modern sense of "metallic food wrap" is from 1946.