A metonymy is a figure of speech in which one word, object, or idea is substituted for another. The metonymy must be close enough related to the original word that the meaning of the substitution is obvious. To a certain extent, metonymies are like metaphors since one word is being substituted for another. The main difference is that metaphors involve the comparison of two otherwise different words. A metonymy, on the other hand, uses a word with a similar meaning to stand in for the original idea.
Let's look at a couple of examples of metonymies from act 2, scene 1 of Macbeth.
Towards the start of the scene, Banquo says, "And yet I would not sleep. Merciful powers / Restrain me in the cursed thoughts of nature / gives way to in repose" (2,1,8-10). In this case, merciful powers is a metonymy for angels.
Later in the scene, during the famous dagger soliloquy, Macbeth addresses the imaginary weapon that seems to float in front of him. He says that "it is the bloody business that informs / Thus to mine eyes" (2.1.49-50). Bloody business is a metonymy for murder.
Shakespeare could have simply chosen to use the word angels in the first instance and murder in the second. However, by employing these metonymies, he made the language more poetic and descriptive. They allow the characters to express their feelings and intentions in a more creative way than if they had used simpler language.