There's three main ways the moons could form: by collisions, by forming with the planet from the initial dust cloud, or by being captured by the planet.
Mars' moons are small, kind of like asteroids. So these were probably captured by Mars from elsewhere. Jupiter (and the other gas giants) probably formed together with their moons, or captured them. (cf https://www.newscientist.com/article/mg20126984-300-cannibalistic-jupiter-ate-its-early-moons/)
The Earth's moon is slightly different, as it is very big relative to the size of the planet. It could have been another "planet" that the Earth captured, but moon rocks show the composition of the Moon is basically the same as Earth. So that implies they formed together, most likely through a collision.
Interestingly, there's a similar idea that Pluto's moon Charon also formed from a collision billions of years ago. (https://scienceline.org/2019/10/unlocking-charons-formation-story/)