The sun, the earth, and the rest of the solar system formed from a nebular cloud of dust and gas 4.6 billion years ago. The early earth was very different from the planet we live on today. The origin of the universe remains one of the greatest questions in science. Early in the history of the universe, matter, primarily the light atoms hydrogen and helium, clumped together by gravitational attraction to form countless trillions of stars. Billions of galaxies, each of which is a gravitationally bound cluster of billions of stars, now form most of the visible mass in the universe.
Stars produce energy from nuclear reactions, primarily the fusion of hydrogen to form helium.
These and other processes in stars have led to the formation of all the other elements.