How is gold formed?

No gold has ever formed in the earth or could ever form in the earth (outside of a few experiments in particle accelerators).

Gold is an element. An atom of gold contains 79 protons, 79 electrons, and (most commonly) 118 neutrons, making it among the most dense of naturally occurring elements.

The big bang and a short period of nucleosynthesis thereafter left the universe filled almost entirely with hydrogen. This gave rise to the first generation of stars, in which nuclear fusion gave rise to ever heavier elements (in ever smaller quantities) up to the mass of iron. When this first generation of stars ran out of fuel, they went supernova, and the extraordinary energy of these and other high-energy cosmic events caused heavy elements already present to fuse into even heavier elements all the way up to uranium.

All the iron, oxygen, carbon, potassium and other elements that make life on Earth possible were formed inside stars. All the gold in the earth was produced in supernova explosions neutron star collisions, and similar high-energy cosmic events.

Gold seams, nuggets, dust, etc. are formed by geologic processes. When the planet formed, most of the heavier metals settled into the core. One way that gold seams form is through the action of deep hydrothermal vents—like those on today’s seafloor, but deep in the crust where metamorphic rock is forming. Gold dissolves in superheated water and is carried up through faults where the water cools and the gold precipitates out into cracks. Geologic processes can then, given enough time, carry the seams to the surface where we can find them.