Crustal deposits of heavier minerals largely result from late bombardment (after the crust had formed, about 100 million years after Earth's formation itself), geothermal and tectonic activity (volcanism, rift zones, vents), and biological mobilisation and concentration (e.g., lighter elements in coal and limestone, but notably, banded iron formations). Events such as the hypothesized Tethys collision could of course also have influenced distribution.
Gold forms veins typically in quartzes through differential precipitation AFAIU.
Ore formation is its own fascinating subfield of geology, and is underappreciated by many advocates of space mining.
https://en.wikipedia.org/wiki/Abundance_of_elements_in_Earth...
https://en.wikipedia.org/wiki/Planetary_differentiation
Those who would like to know more could do worse than pick up Greenwood's & Earnshaw's book (this one: https://www.amazon.com/-/dp/0750633654) from the nearest university library, it has a chapter on nucleogenesis and cosmic and geologic abundances.