"Lose wars" is a subjective term. Sun Tzu explains that the conditions for victory may be different for each side. For instance, in our own War For Independence, we didn't actually have to BEAT the British. We just had to be more trouble to hold on to than we were worth. The British didn't so much "lose" as decide it just wasn't worth the trouble to keep fighting us.
The same thing, more or less, happened to us in Vietnam. We didn't so much "lose" as decide it wasn't worth the trouble.
As another poster has said, it is almost impossible for an invading force to overcome a determined, local resistance. The Germans couldn't do it in France. The British couldn't do it in "The Colonies". It borders on being impossible unless you are willing to resort to methods that, by modern standards, would be called "atrocities" or "war crimes".
The real question is ... did we accomplish what we set out to do?
Well, not in Vietnam, because we never clearly defined what it was we set out to do other than prop up the South Vietnamese government. And that wasn't going to happen without reducing North Vietnam to rubble and possibly starting a war with China.
In Afghanistan, we DID topple the Taliban and eventually killed bin Laden. We weakened Al Qaeda to the point where they are almost irrelevant now. In that sense ... we did win.
Of course, that created a power vacuum (as did our toppling of Saddam Hussein, an action I opposed from the start) which, coupled with Obama pulling out our forces prematurely, allowed the rise of ISIS. (It took TWO presidents to make THAT mess.) So, you could say we won the first war, only to create for ourselves a second. Much like happened when the allied forces "won" WW1.
Of course, the First Gulf War was a "win", technically, but Bush '41 didn't finish the job. He should have either not invaded at all, or gone all the way to Baghdad. And that's another reason the U.S. fails to "win" sometimes. We just don't FINISH the job. We're like the sword fighter who wounds his opponent and then won't finish him off. You KNOW the dude will come back for you when he's healed.
We're like Obi-Wan, leaving Annakin burning by the lava lake. For pity sake, man! FINISH HIM OFF!!! Force push him into the lava ... SOMETHING. But no ... Obi-Wan leaves Annakin there to become the Darth Vader we all know and love. Obi-Wan did WIN the fight ... but he didn't FINISH the fight.
So ... I would say that it's no so much that America LOSES wars ... we just have a lousy habit of not FINISHING wars.