One theory is that moons form when a planet's gravity pulls in nearby material, such as dust and gas, and eventually coalesces into a moon. This process is thought to be responsible for the formation of Earth's moon.
Another theory is that moons form when two or more planets collide and the debris from the collision coalesces into a moon. This process is thought to be responsible for the formation of the moons of Jupiter and Saturn.
Finally, it is also possible that some moons may have been captured by a planet's gravity after they were formed elsewhere. This process is thought to be responsible for the formation of some of the moons of Uranus and Neptune.
Ultimately, the exact process by which moons form is likely to vary depending on the specific planet and moon in question. However, the above theories provide a possible explanation for how moons are formed.