You seem to be forgetting (or deliberately downplaying) a few crucial details about the history of the American military, so let me briefly shed some light on how they 'lost' all those conflicts you mentioned. In chronological order:
Korea: The US joined a war in force (on the other side of the world) as South Korea was literally on it's last legs, pushed the frontlines all the way back through BOTH Koreas right up to the Chinese border, and only had to retreat to the current DMZ because China (who shares a land border with NK) sent 3 million troops into the quagmire. On average throughout the whole war, UN forces were outnumbered at least 2:1 at any given moment and they still would've won if not for the world's most populous nation deciding they really didn't want the US on their doorstep (for good reason). It's a lot easier to supply your troops if they're only a short train ride from some of the largest population centers on the planet, as opposed to 5000 miles away. Even then, both sides experienced major logistical problems throughout the conflict, with almost every action after the first year being reduced to a stalemated war of attrition. Around the same time, Truman's approval rating had dipped to an astonishingly low score of 22%, one of, if not THE, lowest presidential approval rating on record (according to Gallup). Needless to say, he was replaced in the election of '52 and Eisenhower tried to work out a peace deal asap. Then in early March of 1953, Stalin died and while the Soviet leadership fought over who should replace him, they didn't really care about Korea anymore. Which meant they stopped supporting China, which meant China stopped supporting North Korea (at least to the extent they had been previously). Even though the Koreans still cared about the conflict, the superpowers on either side definitely did not anymore
Vietnam: Ignoring all the critical history leading up to American involvement in Vietnam (aka how the French already fucked up the region before we even got there), the US entered a situation where they were entirely unable to utilize most of their strengths and instead had to fight/bomb mostly (though not exclusively) within their 'own' territory. Keep in mind, that even though we refer to it as a war (and for all intents and purposes it was), LEGALLY speaking, we only ever sent peace-keeping forces; we were supposed to be cops, not conquerors. Also worth keeping in wind, Vietnam (the combined, modern nation) is roughly the same size as the western coast of the US, from Seattle all the way down to Los Angeles, that's a LOT of land to endlessly patrol, again, on the other side of the Pacific ocean. And this isn't even getting into the other inevitable problems that arise from fighting an insurgency, especially one where few speak the native language and even fewer are eager to learn. Throw in the equatorial heat, political turmoil back home, and the increasing frustration/desertion rates of the rank and file, and at the end of the day the only thing that made sense was to leave.
Afghanistan: Yet another insurgent war, yet again on the other side of the world, yet again in a surprisingly large nation (roughly the size of Texas) with an exceptionally harsh terrain (almost the whole nation is part of the Hindu Kush mountain range, an offshoot of the Himalayans), except this time you can't even take a boat there; the only real way in and out is through the air, a true logistical nightmare. Add in the fact that Afghanistan was already incredibly decentralized and that its people had been fighting either foreign incursions or each other (or both) pretty much nonstop since at least the mid '70s and it's no surprise that the Taliban and their allies retreated to the countryside (or occasionally across borders) and started using insurgent tactics. They were not, and never have been, just "a bunch of goat herders" as you casually dismissed them. Eventually, the US found and killed Osama bin Laden, who was the main objective behind the invasion in the first place and so, naturally, began making plans to GTFO. A lot of ideas were thrown around and eventually Trump fucked it all up by basically giving the Taliban whatever they wanted, NOT including the 'legitimate' Afghan government in the peace talks, then essentially forbade the military from doing anything except the most bare-boned self-defense actions. We went in with no plan other than "Get Osama" and everything that occurred after his assassination was essentially bad improv but on an international stage
Iraq: I'm not sure where you got the idea that we "lost to a bunch of goat herders" in Iraq too. We fought them out of Kuwait in the Gulf War and beat them badly. Then we invaded Iraq proper in 2003 (for reasons that were admittedly pretty self-serving) and beat them even more badly. Hell, it only took about 3 weeks from the beginning of the war until Baghdad fell and major hostilities ceased (temporarily). Then another insirgency popped up once it became clear that the coalition forces didn't go in with an exit strategy other than "Get Saddam" as well as a few high-profile disasters/fiascos. But even still, once some semblance of order had been restored and the framework for a new government was established (no matter how shaky both those were in hindsight), troops were removed less than 10 years after the initial invasion. They accomplished their mission, and even though there were definitely some fuck-ups along the way, there was nothing left for them to do at the time. Eventually ISIS became a problem in the region, and US troops entered Iraq again but that's even more complicated and to-date they're a mere shadow of the regional powerhouse they were 10 years ago
Iran: Tbh I'm getting tired and I'm still hazy on a lot of the finer details involving the recent US/Iran conflict, especially since the situation continues to develop even as I type this out, but I think I can sum up the loss suffered by the American military in just one word: Trump
In every conflict listed above, the biggest reason why American troops left/'lost' is extremely simple: the war had lost its popularity with the American people and they elected the candidates who were more likely to pull troops out of a conflict that most people didn't approve of in the first place. The Department of Defense is the largest single employer in the entire world, with around 1% of the nation's population working directly for the DoD in some form, as soldiers, beauracrats, or whatever. Like it or not, but practically every American knows someone who worked/served in the military at some point in their lives. These conflicts ended because the friends and family of those people wanted their loved ones to come home, NOT because 'our military lost'