All the gold found on Earth came from the debris of dead stars. As the Earth formed, heavy elements such as iron and gold sank towards the planet's core. If nothing else had happened, that gold would still. be invisible, unknown in the centre of the earth. But, around 4 billion years ago, Earth was bombarded by asteroid impacts. These impacts stirred the deeper layers of the planet and forced some gold into the mantle and crust where we currently find it.
Some gold may be found in rocks. It make occur as flakes, as the pure native element, and with silver in the natural alloy electrum. Erosion frees the gold from other minerals. Since gold is heavy, it sinks and accumulates in stream beds, alluvial deposits, and the ocean.