All of the more complex atoms were created in earlier stars that exploded in the previous 10 billion years. After most of the matter concentrated into what would be the Sun, the enormous amount of pressure caused by the Sun's mass and gravitational field initiated nuclear fusion . Fusion is the fusing together of hydrogen to form helium. During this process enormous amounts of energy are released. The Sun is a continuous nuclear fusion reaction that has been occuring continuously for the last 4.6 billion years.
Planetesimals are small bodies of matter that formed in numerous orbits around the Sun.
With time the larger bodies collided and captured smaller pieces. From these collisions the planets formed. Earth's Moon probably formed when a Mars-sized planetesimal collided with Earth. After the collision some material was ejected into Earth's orbit. Most of the material returned to Earth.