Contrary to some things that have been posted here, the US did not wear green coats, or green uniforms, during the Mexican American War, so that explanation for the origin of this word is bogus.
But the term is indeed related to American soldiers who invaded Mexico during the Mexican-American war. It actually came from the lines of what was then a song that was very popular with American infantrymen.
See the example.
A popular American song during that time--one sung by many American troops as they marched--contained the repeated refrain:
"Oh, the ash and the oak and the willow tree,
And green grows the grass on the infantry"
Being members of the infantry, the second line was sung with particular fervor. "Green grows" sounded like "gringoes" to the non-English-speaking Mexicans, so they used that term to refer to all of the American soldiers.