There are a few theories about this. One is that a huge asteroid hit earth and some of the fragments blown out of the planet became the moon. Another is that the moon formed at the same time as the earth, from dust and rocks in the early solar system. Yet another is that the earth wasn’t really round when it was young and so a bit split off because of the spin and became the moon, and finally the fourth is that the earth’s gravity captured the moon – it might have been an asteroid or similar and got caught and became our moon.
Scientists can look for evidence to support these theories – if the moon is made of earth stuff, the rocks at its heart should be very similar to earths. If we captured an asteroid, the rocks might be very different. We probably need to drill into the moon to get more information.