The heavy elements, that is heavier than helium, were most likely built up proton by proton inside a star, which then exploded--supernova--distributing its contents sort of uniformly over a volume of several lightyears. That material, mostly gas and dust, would have condensed by gravity into the sun and what would become the rest of the planets and other bodies orbiting the sun. Since the gas remaining would have been blown by the force of the suns burning outward faster than the dust, the portion of dust remaining near the sun and becoming Mercury, Venus, earth, and Mars would have collected and condensed more or less uniformly. Later the materials would stratify to a degree as the planets settled down, with the heavy portion--iron and nickel-- at the bottom and the light materials on top. It is possible gold settled to the very core, although some think uranium has concentrated enough down there enough to start reacting. The gold in the crust is what did not settle into the core, and that it is concentrated in placer deposits is kind of interesting.