Lesley D will undoubtedly know better than I.
That said, here is what I have heard.
Some of the first foreign visitors to the Iberian peninsula were seen to be "Greeks" and refered to as "griegos" which morphed over time to sound like "gringo".
Others say it was coined by folks wanting the US forces OUT of their countries, "Greens Go".
Either way, it usually is used by Spanish speakers to refer to pale foreigners.
Sometimes it is nice, sometimes it is an insult.
Here is a link to a good article.
Enjoy the controversy..............Sam