The Big Bang
No one knows why it happened, but the evidence that it did happen is overwhelming. The universe was created in a huge explosion. The energy released in the explosion was enough to create matter. There was a gigantic cloud of hydrogen and helium, with just a tiny bit of lithium thrown in. That was it.
First Cloud Makes the First Stars
Like all matter, the hydrogen and helium atoms pulled towards each other through gravitational attraction. Once a few atoms were close to each other, they exerted an even greater gravitational attraction; and so, a few atoms at a time, large masses came together within the cloud. The force of gravitational attraction was not enough to overcome the outward push of the Big Bang, so instead of forming one ball out of all the hydrogen and helium, billions of separate clumps of matter accumulated from the original cloud. Those were to become the first stars.
Stellar Fusion
As a clump of hydrogen and helium grows, the temperature in the interior rises: the atoms are pushed towards each other more strongly. When the temperature reaches a certain point, the electrons are stripped away from the atoms, leaving just their nuclei swimming around, bouncing off one another. As it gets even hotter, the nuclei don't always bounce off --- they stick to each other, fusing to create new atoms. When they fuse, they release energy. That release of energy is what defines a star: a ball of matter that creates its own energy through nuclear fusion. Those first stars started as just hydrogen and helium, the matter that came from the clouds of the Big Bang.
New Atoms are Created
Stars --- especially giant stars --- go through many different stages of existence. Those stages are defined by the predominant type of fusion taking place in the core. The first type of fusion creates helium from hydrogen. When the hydrogen runs low, the helium fuses to create other atoms. One chain of fusion creates carbon, nitrogen and oxygen; another creates elements all the way up to iron. That's where stellar fusion stops --- there's not enough energy to create elements heavier than iron. But when giant stars explode, they release even more energy, energy that can create heavier elements: copper, iridium, barium, uranium, tin, lead and many more were all created in supernova explosions and sent out into the universe to join the clouds of hydrogen and helium.
Cloud That Formed Our Planets
The cloud that formed our planets was composed of hydrogen and helium from the Big Bang, more hydrogen and helium ejected by stars during their lifetimes, even more hydrogen and helium ejected from exploding stars, and --- finally --- a tiny bit of heavier elements sent out from those same exploding stars. As the sun formed and started its own nuclear burning, it pushed hydrogen and helium away, leaving the heavier elements close to the sun to form the rocky planets of Mercury, Venus, Earth and Mars. Further out, the hydrogen and helium condensed to create the gas giant planets of Jupiter, Saturn, Neptune and Uranus.