You can trace the origins of grey and gray back through the history of the English language and its interactions with other languages.
Both words have their roots in the Old English word "grǽg," which people used to refer to the color gray well before even the 12th century. Geoffrey Chaucer, the 14th-century poet, use the spelling "greye" to describe the color of a woman's eyes in at least one Middle English poem.
By the 18th century, the famed British lexicographer Samuel Johnson had created a dictionary that included the word gray as well as grey, although he made it clear he preferred "gray" by giving it the more comprehensive definition. Despite this, use of both spellings persisted throughout the 19th century, with grey eventually overtaking gray.
By the 20th century, gray was the favorite spelling in the United States, while other English-speaking countries preferred grey — preferences that remain in place today.