(n.b. This explanation considers the heating of water and grains, particularly oats.)
The answer begins with the fact that the water in the pot is not pure water, but a mixture of water and gases from the air, such as oxygen and nitrogen, which are dissolved in it. Also, as soon as you put the grain into the water, it begins to hydrate, swell and weaken. At this stage, water-soluble proteins will leach out and dissolve in the water.
When you light the stove, the water in the pot heats and its structure loosens, releasing the gases, which form bubbles, particularly at the bottom of the pot where the temperature is highest. Similarly, when quantities of the water reach boiling point (100 degrees centigrade at sea level, but about 90.5 degrees at an altitude of 2,850 metres, where I am now), bubbles of water vapour (or steam) are also formed.
These bubbles are inherently unstable and most would collapse or dissolve before reaching the surface of the water. However, as the grains are warmed in the water, starches also leach into it along with proteins that are less soluble. These lose their shape in the heat and bond with the starches. This begins the process of turning the water and grains into a thick protein-rich, starchy solution.
The proteins, including the water-soluble variety mentioned earlier, have parts which are hydrophobic and parts which are hydrophilic. The hydrophobic parts are attracted to the bubbles of gas and stick to them, while the hydrophilic parts are oriented outwards. This creates a coating which strengthens the bubbles and makes it easier for them to reach the surface, where they do finally burst, depositing their water vapour and starchy coatings, which creates foam. The starchy solution has a lower surface tension than water, which also promotes stable bubble formation.
The next part you probably know. The foam builds on the surface of the solution and would cover the surface completely, except that the continuously rising bubbles disturb the foamy layer even as they contribute to it. However, if the solution is hot enough, then enough bubbles will be formed that the layer of foam thickens until it forms an unbroken cap.
The water vapour being delivered to the surface of the solution by the bubbles is then trapped under the foam. It also, together with the pot, creates a complete layer of insulation around the solution. The heat cannot disperse and its temperature is limited to boiling point, so instead the heat gets distributed more extensively through the solution, which accelerates the formation of water vapour bubbles.
The intense heat and rapid movement of gas to the surface causes a build up of pressure under the foam, increasing the volume of the water vapour and pushing the foam upwards until it spills. This breaks the foam layer, releasing the water vapour and allowing the built-up heat to dissipate. The solution then returns to its previous level, and the cycle of forming a foam cap and boiling over resumes until a human intervenes and turns down the heat, probably swearing under their breath as they do so.
This concludes my explanation of why pots boil over. I hope you found it interesting.