Around 4.5 billion years ago, when the Solar System was brand new, Earth shared its orbit with another planet. This planet, Theia, was about the size of Mars.
The thing is, planet-sized objects can only share an orbit in a stable manner if the larger planet is at least 25 times more massive than the smaller one. Earth was only around 4 times as massive as Theia, so Theia’s orbit destabilized and the smaller planet began falling towards Earth.
Theia struck Earth and exploded, instantly liquefying Earth’s surface and blasting massive amounts of debris into orbit.

Over time, much of this debris fell back to Earth, and some more was thrown out of Earth’s orbit to form asteroids, but a significant amount of the debris formed a stable orbit around Earth in the form of a ring system.
Over time, the debris in the ring system began to gravitationally clump together, eventually forming the Moon. It’s quite possible that some of these clumps formed small moons before eventually being pulled into the Moon itself.
