I understand that the heavier elements were produced by a series of fusion reactions in a series of stars. What I've never understood is why the atoms of these elements would ever coalesce together so that we can find, for instance, veins of gold or uranium or whatever here on Earth (or anywhere else we might someday mine). How and why did large deposits of anything ever form?
Water-soluble uranium compounds are spread pretty uniformly through the Earth's crust. Water seeping through the soil and rocks picks up those compounds. The solubility drops, though, when there is very little dissolved oxygen in the water. So, when water ends up in a stagnant swamp where the oxygen concentration gets very low, uranium compounds precipitate out and settle on the bottom. If the process continues for a long time, substantial deposits of uranium minerals can be formed. See, for example, Gabon Natural Fission Reactor.
Gold gets concentrated in various ways. An important type of gold deposit is placer deposits: tiny grains of gold that can get hammered together by stones rolling in a stream. The gold in this case often does not start out as tiny grains. Instead, it is in a dissolved form, leached out of rocks. Hydrothermal activity around volcanic calderas results in water chemistry that does a good job of dissolving gold. Certain kinds of bacteria gather dissolved gold from water and actually form microscopic gold shells or particles The gold then is further concentrated by processes analogous to centrifugation, which segregate particles according to their mass density.
For objects that are large enough so that the gravitational energy released during their coalescence from dust and gas is sufficient to make them melt, the heavy metal concentration process will occur naturally in the (molten) core of that body as gravity sorts the constituents according to their density. The heavy stuff sinks to the center and the lighter stuff floats to the surface and forms a less-dense crust.