The dominant theory explaining the creation of the Moon is the giant impact hypothesis, which argues that 4.533 billion years ago (only about 70 million years after the formation of the Earth itself), a Mars-sized body named Theia collided catastrophically with the Earth, throwing many trillions of tons of magma and rock into Earth's orbit, which coalesced and cooled to become the Moon. This theory is not perfect, however — it predicts that the Earth had a magma ocean at one time, for which no evidence can be found.
Early in the solar system's history, when the planets were just forming, the solar system was a more crowded place. Orbits were still stabilizing, and many of the early asteroids degraded into unstable orbits that fell into the Sun and Jupiter or got ejected into the far reaches of the solar system. One gravitationally stable point for the coalescence of space rock, the L5 point, had a special harmony with the Earth. Located within Earth's orbit but occupying a different position than the Earth itself, L5 is the present-day home of the Trojan asteroids.
As material coalesced at L5, it is eventually thought to have exceeded the mass of Mars, which caused its path to become unstable, oscillating along the length of Earth's orbit rather than staying put at L5. Soon enough, this oscillation brought it right into contact with the Earth, in an event called the big whack or the big splash. Tons of material was ejected, and after 1 to 100 years this material coalesced to become the Moon, which was initially covered with magma oceans. Evidence for these oceans was found by the Apollo astronauts.
The Moon is peculiar for orbiting the Earth because of its large relative size. Venus, a planet of similar size to Earth, has no Moons despite its similar mass. Mars has only a few, which are very tiny, with radii less than 20 km. So Earth's astonishing Moon, among the largest of the solar system, requires a special explanation, given by the giant impact hypothesis. Observations that the Moon's composition is similar to that of the Earth's crust, but not our mantle, led scientists to formulate the giant impact hypothesis.