In rhetoric, metonymy is the substitution of one word for another with which it is associated. Metonymy works by contiguity rather than similarity. Typically, when someone uses metonymy, they don't wish to transfer qualities (as you do with metaphor). The common figure "The White House said..." is a good example of metonymy, with the term "White House" actually referring to the authorities who are symbolized by the White House, which is an inanimate object that says nothing. The Crown for a kingdom is another example of this kind of metonymy. Metonymy can also refer to the rhetorical strategy of describing something indirectly by referring to things around it: describing someone's house in order to describe them, for example. Advertising frequently uses this kind of metonymy, simply putting a product in close proximity to something we want (beauty, happiness). See also figure of speech, synecdoche, metalepsis.
In cognitive linguistics, metonymy is one of the basic characteristics of cognition. It is extremely common for people to take one well-understood or easy-to-perceive aspect of something and use that aspect to stand either for the thing as a whole or for some other aspect or part of it. For example,
The pen is mightier than the sword.
"Pen" and "sword" denote publishing, and military force respectively.
See also: Social stereotype