The Earth (and the other planets in the
Solar System) formed by the accretion of gas and dust
out of the protoplanetary disk.
http://cosmos.colorado.edu/astr1120/l9S4.htm
At the very end of this process, there were around
10 large planets -- more or less what we have now in
the Solar System -- plus a large number of smaller
objects called planetesimals. Planetesimals
are (were) like very large comets and/or asteroids
which could easily have been 10s to 100s of kilometers
in diameter.
It probably took something like 10s of millions
of years for the planets to form.
Near the end of this time, the Moon
was created through a giant impact with the Earth.