The first recorded mention of Yankees is in 1758 by British soldiers in North America, referring to local American colonists. Most sources I've read over the years ascribe the origin to English slang for the earlier Dutch settlers in the Hudson River area (following the British capture of New Amsterdam and renaming as New York in 1674).
One version says the English referred to the Dutch disparagingly as "Jan Cheese" (pronounced Yahn, the first name of my own earliest Dutch ancestor in America, Jan Speer, his farm now a museum in Montclair, NJ). Jan Cheese gradually became slurred into Yankee, with the British a century later using it to refer to all colonists, both English and the earlier Dutch.
Another version also relies on the "Jan" but says it was combined with another Dutch name, "Kees" to form the English colonial slang for the Dutch.
A third version says it originated with the Indians who heard the French refer to the English as l'Anglais, which the Indians corrupted into one form, that the colonialists later further modified for themselves. I find this the least plausible explanation.
Since the late 18th Century the term has most correctly meant people living in the New England states, mainly north of New York City. In the Deep South it came to mean anyone from the Northern States, and was uniformly applied to all soldiers in the Union Army during the Civil war by both sides in the conflict, regardless of their home states.
Personal anecdote I've told here before: while attending a wedding in Richmond, Virginia, over a 3-day period in 1977, I was introduced to the groom's grandfather. The groom's mother did the honors, telling the family patriarch that I was "Bob frum the Noth." At which point grandad yanked his hand away from mine, angrily exclaiming "Damn Yankeh!" and turned and walked away, leaving me with hand outstretched. It took some frantic persuading by the rest of the family to drag him back and apologize to me, which I accepted with good grace, though I didn't believe a word of it.