Surfactant is a kind of soap-like substance that nullifies the surface tension in the alveoli. The surface tension is created by the thin film of water lining the alveoli.
You may have observed that some insects walk on water? And if you’re careful, you can get a needle to ”float” on the surface of water. It is as if there is a film on the water surface that insects and the needle floats on. The explanation of this is that at the water surface (where water meets air) water molecules attract to each other more strongly with the water molecules below them than with the layer of air immediately at the water surface (remember that water molecules are dipoles which means they have opposing charges at each side of their molecular structure so they can attract each other like magnets). The water molecules pull on each other, creating a surface tension which as acts like a film on the water surface.
If a drop of soap or detergent is dropped into the water, the needle sinks immediately. The detergent such as phospholipids are both fat-soluble and water-soluble, If they are added to water, they align themselves on the surface of the water, because the fat soluble part does not dissolve in water and is repelled away from the water. This layer is therefore left in the air. In this way the detergent disturbs the water molecules that form the surface tension so that the needle does not keep afloat.
In the alveoli. the film of water inside them creates a surface tension which could collapse the alveoli. To counteract this, the type II cells in the alveoli membrane secretes surfactant that reduce the surface tension of the water, and thus prevents the alveoli from collapsing.
Surfactants in the lung are produced only at the very end of fetal life. Lack of surfactant is therefore a real problem in premature babies. Their immature lungs will remain collapsed due to the surface tension in the alveoli. This can be treated with surfactant-like medication.