Up until Vietnam, the US had been training their military to fight a "conventional" war, more along the lines of what they fought in each of the World Wars. In Vietnam, the fighting was very "unconventional". In each WW, they could easily identify the enemy because they wore different uniforms and spoke a different language. In Vietnam, the enemoy spoke a different language, but it was the same language as the military they were supporting. Furthermore, their enemy did not customarily wear uniforms. It was easy for them to melt into the surrounding populace without being clearly identifiable.
Another factor was the guerilla warfare component. Much of the fighting in Vietnam involved night raids by the North, as well as surprise ambushes and quick assaults that melted away into nothing. What I mean by that is that the enemy would seem to disappear because they were so adept at blending into the landscape and they also made extensive use of tunnels to allow them to move troops and supplies. The US was ill equipped and poorly trained in how to fight a guerilla war.
When you look back in the history of the US, you could ask the same question about how the US was able to defeat the British during the Revolutionary war. The answer is basically the same. The US did not fight a "conventional" war, they were better able to make use of their surroundings and the terrain, and they fought a number of smaller skirmishes rather than trying to launch a full out attack against superior forces.
In both scenarios, there was definitely an advantage to fighting on your own soil. Your supplies are more easily replenished and it is easier to muster support from the rest of the country. In both cases, the home team just had to outlast the willingness and desire of the "invaders".