Gold is very difficult to produce to start with, hence it is rare for that reason. It can be produced in neutron star mergers or super nova explosions.
Read Also: This Poison Is Guaranteed To Give You Cancer(Caution)
Gold is also very heavy, so it is believed that all gold present when the Earth was molten sank to the center of the Earth, and that all gold on land was delivered by asteroids after the Earth surface had solidified.
In addition we have continental drift with subduction where a plate is going down to be recycled in the interior. Any gold there is likely lost by sinking to the center of the Earth, but can in some instances surface again through plume-subduction interaction.