Ocean water isn't entirely water. Fill a clear glass with some sea water you'll see that it's full of tiny particles — dissolved salts, proteins, fats, dead algae, and a bunch of other bits and pieces of organic matter. Now, shake the glass vigorously, and small bubbles will form on the surface of the liquid.
This is pretty much how sea foam forms — but on a much grander scale — when the ocean is agitated by wind and waves. Each coastal region has differing conditions governing the formation of sea foams.
Algal blooms are one common source of thick sea foams. When large blooms of algae decay offshore, great amounts of decaying algal matter often wash ashore. Foam forms as this organic matter is churned up by the surf.