When were the heavy metals in earth formed?
By the largest stars in the universe.
Stars the size of ours get to a point where they run out of hydrogen and start burning helium. This is when the become red giants. It is the continuing process of nuclear fusion, two atoms of hydrogen fuse together to form a single atom of helium etc. This fusion continues until iron is formed and then it stops dead.
But there are a lot of stars much larger than our sun. There is one star which is 1300 times larger than our sun. But this star will live and die in a relatively short period of time. The life of a star is inverse to its size.
When these giant stars swell outwards towards the end of their lives they go through the super-nova stage before they start collapsing in on themselves. Gravity, as always, is the culprit. But the nuclear fusion which has been happen right along continues right through the creation of iron and on to the rest of the natural elements. It is in these super novae where metals such as gold, silver, platinum, uranium and other heavy metals are formed.
When that star finally explodes it expels many of these heavy metals into space which eventually find themselves to other stars in their constellation.