The key is early introduction. This is the first step towards learning. As your baby is exposed to the water, they discover the water environment and freedom of buoyancy. Activities in the water also helps to develop their gross motor skills, coordination, balance and confidence.
From birth, babies have natural reflex called Diving Reflex (Gag Reflex), which allows them to be submerged without swallowing any water. When your baby is submerged underwater, their epiglottis closes. Early introduction to the water allows your baby to use this reflex and progress from involuntary muscle action of holding their breath underwater.
Although there is a small percentage of babies that do not have this capabilities and the diving reflex might disappear from 2 to 6 months, some babies maintain them till much older. This varies from baby to baby. Discuss with an experienced baby swimming instructor to know if your baby still has this reflex.
Babies who are exposed to swimming and being submerged before 6 months have an early start in holding their breath. When they get older, they will slowly learn how to hold their breath and not depend on their reflex. The length of each submersion can gradually increase with more practise on their breath control. However, babies should not be submerged for too long. It's highly encouraged that you practice this with a professional coach.