Proteins dry out and bond on the surface.
When you heat milk, water evaporates from the top. That leaves milk proteins, mainly casein and whey, along with some fat, concentrated at the surface, where they dry out and bond into a thin, rubbery sheet. Once a skin forms, it also traps steam underneath, which is one reason heated milk can suddenly boil over.
You can stop it a few ways. Stir often while heating, so no layer stays still long enough to dry. Keep the heat moderate rather than high. Cover the pan with a lid. For custards and puddings, press plastic wrap or parchment directly onto the surface while they cool.
The skin is perfectly safe to eat, and some cultures prize it. In India, malai, the rich skin and cream from simmered milk, is collected for desserts, and in Turkey kaymak is made in a similar way.