The word was used by U.S. Marines in the early 20th century; the earliest written example is dated 1920.
Folk etymology suggests that during the Korean War, young Korean children would point at U.S. soldiers and shout in Korean 美國 ("America"). Soldiers heard the word as “me gook”, as if the children were defining themselves as “gooks”. The soldiers proceeded to use that term to refer to the Koreans. The word itself simply means “country”. This explanation ignores the fact that there are many examples of the word's use that pre-date the Korean War.