First, the nascent universe had to cool such that nucleons formed, which then became hydrogen. This matter wasn't evenly distributed. Some of it under its own weight collapsed under gravity and birthed the first stars.
It seems that shock waves in nebulae trigger star formation. This could be black holes and/or neutron stars merging, supernovae and possibly other events.
These stars became the nuclear furnaces for heavier elements. The death of these stars spread these metals (in astronomy terms; anything heavier than Helium). This cycle continued until there was enough matter in the protoplanetary disk to form rocky planets, of which we are one (of many).
This article talks about the nuances of how and when carbon became part of this system. While that's certainly interesting, it's the heavier elements (than iron) that I find more interesting. Anyone with rudimentary physics knowledge knows that this path only forms elements up to iron because beyond that it consumes energy.
So heavier elements were seeded in this process through supernovae and, as we've confirmed in recent years, from neutron star mergers. So the right sort of stars need to form in the right way and die and then their remnants need to merge to scatter these elements.
I saw one study that suggested the uranium on earth formed by such an event 80-200 million years before the Earth did. That's kind of crazy to think about.
We are literally made of stardust.