Have you ever poured a beer into a glass, and had to wait for the foam to go down before you could drink it?
Well next time, stick your finger in it.
When you open a can of beer and pour it into a glass, dissolved carbon dioxide and nitrogen escapes from the beer as tiny bubbles of gas. The gas bubbles rise to the surface and create the foam we call a "head."
Beer is made from fermented grain, and proteins from these grains contribute to how vigorous the foam head will be. Each molecule of these grain proteins have two ends with different chemical properties; one end is attracted to water, while the other is repelled by water.
As gas bubbles emerge from the beer, nearby protein molecules rearrange themselves with their water-avoiding ends sticking into the gas bubble, away from the liquid. At the same time, the water-loving ends stick out into the beer.
Each bubble of gas is surrounded by a layer of protein molecules which are all lined up in the same direction, strengthening the surface of the bubble.
That being said, how does sticking your finger in the glass keeps the foam from overflowing?
You probably know that oil and water don't mix. Oils from your skin stop the foam from overflowing by breaking up the protein layer surrounding each gas bubble. As the water-avoiding ends of the proteins are more strongly attracted to the oil than to the gas they pull out the bubble, weaken its surface, and cause it to pop.