rey, ley, grey, buey
I believe this is due to the plural forms of buey(es), grey(es), ley(es), rey(es).
These are the only Latin-descended words in Spanish which end -y. With the exception of buey (which evolved by semantic analogy to grey), their Latin precursors all ended in -egem etc, which evolved /egem/ > /ege/ > /eje/ > /ej/. Thus they are the earliest (and only 'naturally inherited') Spanish words ending in a stressed /ej/.
Over the centuries there was free variation as to whether to spell the singular forms with a terminal -i or -y. Indeed rei, lei etc appear as alternate orthographies in several dictionaries from the 15th - 19th centuries (NTLLE):
Tambien la usan en Cast. en algunas ocasiones como en ley, Rey, aunque sea mera vocal, y si bien aqui no habia equivocacion, y se podia conservar, seguimos á muchos que la quitan, y escriben Lei, Rei, &c.
- 1788 TERREROS Y PANDO (P-Z)
Given their plural forms end in /ejes/, and intervocalic /j/ is usually1 2 written -y-, it seems natural to assume the -y singular forms won out by analogy to the relatively stable plural spelling.
Note that in Old French, Old Portuguese, and Old Occitan we do see orthographies in -y. However, in most modern romance languages (Spanish being a notable exception), the plural of these words are -eis or -es, thus lacking the /VjV/ impetus for a distinct orthography.
Later -y words
Once the orthographies of the above Latin-origin words had been established, it's natural that any new Spanish words ending in a stressed /Vj/ would suit the same orthography.
Such words fall into a handful of different groups:
Derivatives
- detienebuey, matabuey, vacabuey, güey, virrey/visorrey
Contractions
- estoy, doy, soy, voy
- ahí, valahí/velay, hay (< ha i < ha ahí)
- hoy, doy (< de hoy), acroy, choroy, coicoy
Apocopation
- fray/frey (freile < fraile), muy (< muito > mucho), taray (taraje < tarahe)
- cucuy/cocuy (cocuyo), pijibay (pejibaye)
Onomatopoeia
- ay, ayayay, ajajay/jajay, alalay, caray, huy, uy, picuy/pijuy, pitoitoy, guay, guirigay
Foreign loanwords
quechua
cacuy), chuchuy, arrarray, atatay/tatay, pacay, huacatay, chachay, ananay, amancay, achalay
mapuche
cuicuy, quilmay, quillay, colliguay
guaraní
timboy/timbó, urunday/urundey, urunday, ñandubay, curupay, aguaribay; tupí
margay; paipay
paipái/paipay
taíno
caney, carey/caray, copey, mamey, maguey; caribean
batey, quibey
Cuban flora & fauna
catey, ..., curamagüey, curujey, guararey, jagüey, yarey
tagalo
sangley, sinamay, barangay, anay;
turkish
dey
greek
escay
french
bocoy, convoy, lay, estay, contraestay, ensay; portuguese
balay
english
boy, corderoy, yérsey, yóquey, vóley, póney, monterrey, tubey, órsay, gray, gay, espray, cambray; dutch
coy
Though there are a handful exceptions in -i, almost all of them have alternative orthographies with -y:
latin
agnusdéi
french
espahí/espay, chií, cai, rai
japanese
bonsái, samuray/samurái, haikai/hai-kai
english
masái
quechua
cocuy/cocui, cuy/cui
guaraní
cay/caí, tipoy/tipói/tipoi, yatay/achachay/yataí, bacaray/vacaray/vacaraí
pascuense
moái
caló
chai, jai
Others
Notes:
1. Exceptions are exclusively learned greek loans e.g. elaiotecnia, meiosis, pereion, paranoia or demonyms hawaiano, amambaiense, ushuaiense.
2. Y. ... nos sirve de letra consonante, quando he de herir á la vocal siguiente: como en Yermo o Mayo.
- RAE 1739