I learned Korean for the Air Force (Well, I tried, anyway). Our instructors were Koreans who'd become U.S. Citizens. One of our instructors gave us a story one day as to how "Gook" came about as a term for Koreans (now a pejorative).
He stated that the Korean words for some countries contain the syllable Kook, with the first K being pronounced more like a G). Korea is Tae Han Min Kook, or sometimes just Han Kook (think of Hankook tires). China is Chung Kook. America is Mi Kook (pronounced "Me Gook").
He then said that when American troops were hitting the ground there during the war, many of the Koreans came running up shouting "Mi Gook! Mi Gook!" as a way of expressing relief and welcoming the troops.
The troops, being the GI's they were, basically said, Okay, you're a Gook...
His story, not mine.
It may be true. It’s one of the possibilities listed in Wikipedia.