Carl Sagan of Cornell explained this in one of his books I read. Intense gravity can cause a star, composed of elemental hydrogen and helium, of a certain mass to collapse upon itself, producing heavier elements (like gold) in the process. Then it blow out all the mass and this mass slowly agglomerates into a new star and planets. Sagan believed that there was another solar system prior to the current one whose detritus then settled into the current form, heavy metals included.
Experiments aboard the Space Shuttle, viewable on Youtube, demonstrate how electromagnetic forces naturally cause fine particles of matter to clump together in the zero gravity environment of space. The Solar System has a plentiful supply of asteroidal and cometary debris with which to replenish planetary rings for billions of years to come.