Foam on the ocean

Before we can understand foam we need to talk about molecules, starting with water.

Chemistry does not seem to be a popular subject now. When you go to book stores there aren't many books on chemistry in the science section. I didn't like chemistry when I was in college. It didn't have the mystical charm of biology or the intellectual status of physics, and I could never remember the names of organic compounds. That's too bad because it really is interesting and also necessary for understanding many aspects of the world we live in. Maybe this section will nudge you toward that belief also?
Each water molecule contains one oxygen and two hydrogen atoms. The bonds that hold these atoms together are partially ionic, which means that the atoms have a partial (non-integral) electronic charge. The oxygen is negative compared to the hydrogen atoms, as seen in the Figure to the left. The water molecules tend to align themselves so that a negative oxygen is next to a positive hydrogen. The alignment is not completely organized or regular, and the individual water molecules are moving around and thus changing their neighbors. However, even though water is a rather fluid material (pun intended), water molecules "like to be next to" other water molecules (in physics-speak it's a lower energy configuration).
An air-water surface is not a happy place for water molecules. They can only interact with other water molecules below the surface, not above. There are few water molecules above the, only a thin water vapor and a sparse collection of nitrogen and oxygen gas molecules that they can't react with anyway.

The unhappiness of surface water molecules is labeled "surface tension" by humans. They imagine a stretched rubber membrane at the surface. This causes the water to move (subject to other forces such as gravity) in such a way as to minimize the area of the surface.

Air bubbles at the surface represent more air-water surface, and the water in the film that forms the bubble is quickly reabsorbed into the main body of water: the bubble pops.

Oil is hydrophobic

Oils are members of a class of molecules that don't mix well with water. Oils are hydrocarbon chains containing about ten carbon atoms. The middle sections of two oil molecules are drawn on the right. Oil molecules bind to each other like water molecules do, but with much weaker forces because there are no significant electronic charges on the atoms.

If the chains are only 1, 2, 3, or 4 carbons long, methane, ethane, propane or butane, the molecules bind to each other so weakly that they form gases at room temperature. If the molecules are 20 carbon atoms long they bind so strongly as to form waxes. In between these two classes of hydrocarbons are the oils. As you might imagine there are variations on this theme. If some of the hydrogen atoms are missing on adjacent carbons they can form "double bonds", and these oils are called "unsaturated". As you guessed, oils with only single bonds are called "saturated".

Thus molecules that are non-ionic and don't mix with water are hydrophobic while molecules that are ionic do mix well with water are thus called hydrophilic.

Soap is amphipathic (or amphiphilic)

Some molecules are chimeras: one part is hydrophilic, the other hydrophobic. That is the meaning of amphi- (both) pathic (feeling). Soaps and detergents are examples of amphipathic molecules.

The molecule on the right is part of a soap molecule (the hydrocarbon chain is actually a few carbons longer than shown here). It has ionic (charged) atoms at one end and an oil-like hydrocarbon chain at the other. A soap solution is great for washing oils from things, the hydrocarbon tail mixes with the oil and the ionic end mixes with the water. This effectively makes the oil soluble in the wash water and it goes down the drain.

Soap is also good for stabilizing an air-water surface. As seen on the right, even a one molecule thick soap film keeps water happy because the hydrophilic ends of the soap molecules line up in the water. The hydrophilic ends of the soap molecules are packed together, which keeps them happy too. The air-water surface has be eliminated.

This configuration is why soap bubbles stay around for quite some time.

Many biomolecules are amphipathic

Proteins are long polymers containing 20 different kinds of amino acids. Some amino acids are hydrophilic, and have either positive or negative charged groups. Many other amino acids are more oil-like, and are hydrophobic. In the native state the amino acid polymer (or polymers) that make up the protein are tightly coiled up, with the hydrophilic amino acids on the outside. Thus the native protein molecule is seen to be hydrophilic to the outside world, and is soluble in water.
Protein molecules can be denatured or unpacked, by heat, drying, or agitation at an air-water surface. In the denatured state the polymer of amino acids is unwound, exposing most of the amino acids to the environment. Now the protein is seen as amphipatic, with exposed hydrophobic as well as hydrophilic regions. It may become insoluble and form visible clumps and strings. This is what you are doing when you cook an egg.

Denatured proteins typically have a mild soap-like activity, and thus stabilize air bubbles. Thus, protein solutions produce foams if there is sufficient mechanical agitation.