With the birth of our universe, energy was released that interacted with itself eventually forming simple atoms like hydrogen and helium as the universe cooled (in about 380 thousand years)
As matter formed, Nebula (huge dust and gas clouds of the matter) form and create stars as energy and matter are brought together by gravity (which fosters larger concentrations of energy to coalesce).
Stars are eventually born in these Nebula and they fuse hydrogen into helium, then carbon, then oxygen and the other essential elements of life, in their energy intense cores.
When a stars fuel is spent, it releases elements into the space around it up to the energetic level of iron (elements 1-26), from the explosions of larger stars high intensity energy expressions, or super novas.
The more energy dense the element, the more time and/or energy that is required to form it.
Even the largest stars’ super novae however, cannot create the gold and denser energy rich elements!
The heaviest elements require more, they require two lesser stars exploding and creating proximate Neutron Stars that dance for awhile and eventually collide and create the gold and higher density energy elements through their intense energy concentrating gravity effects.
To illustrate the density of a neutron star, they are the most dense concentrations of energy in our universe. Larger super nova explode leaving Blackholes, which literally escape the confines of our universe, but neutron stars remain. Neutron stars are approximately 7 miles in circumference, but have the energy mass of millions of times that of the earth.
A matchbox sized container of a neutron stars matter would weigh about 5 billion tons!
Before Earth was formed at least two generations of nearby stars, including super nova and pairs of nearby neutron stars, had to form, die, explode, reform, die and explode again to provide the necessary elements to coalesce and form it.