They are earned by racers who win races either during arcade hours or during the nightly race to determine which of the Racers become the next day's avatars. Apparently, the Random Roster race, and presumably others, are pay to play. In order to race, you have to pay one gold coin to be allowed to race. Gold coins are also apparently equal to the Medal of Heroes in worth, as Ralph's allowed Vanellope to qualify for a race. This suggests that something valuable besides gold coins can buy one a chance to race. Vanellope, having none of her own, gets around this when she discovers Ralph's hero's medal, and thinking it was a gold coin uses it to buy her way into the race. This causes Ralph to join forces with her so that she can win the race; as a result, they are able to recover the medal.