He failed to win because he was out out fought by better generals, Lee, Jackson and Longstreet, he failed to win because AP Hill drove his men past exhaustion for 9 miles as the crow flies from Harpers Ferry to Sharpsburg with these underfed, barefoot, thirsty and exhausted rebels forced marching to the sound of guns, many passing out along the way.
All day Lee had masterfully defended his interior lines against overwhelming odds, shifting assets from one threatened point to another until the close of day when he was out of troops to shift and the Yankees were finally breaking through at Burnside Bridge.
The scene is as dramatic as it gets, Lee seeing dust to the south and east, asks a younger officer with younger eyes, " what is that dust there"? The officer replies, "Yankees." Lee points to another, dust column and repeats the question. The officer replies, "they are Confederates, sir."
Lee's only comment, "it's AP Hill come from Harpers Ferry." He might have added AP Hill is coming to the survival of the Army of Northern Virginia, to the survival of the Confederacy and AP Hill comes to end America's single bloodiest day. The exhausted men slammed into the Yankee flank to end the bloodletting.
Both Lee and Jackson fought at the ragged edge of disintegration throughout the day. Indeed, few American generals, perhaps George Washington among them, have so often been put to this ultimate crisis as have Lee and Jackson. His composure in the face of seemingly inevitable disaster measures Lee's greatness. It is his integrity, among so many other character attributes, that distinguish R E Lee and George Washington as America's greatest generals.
My ancestors were there defending Dunker church and the cornfield under Stonewall Jackson. This evening in 2022 on the anniversary of this battle, I expect to participate in a class reunion when the question that haunts is how would the revelation that my great-grandfather and great uncle were with Stonewall Jackson at the battle of Sharpsburg in America's single bloodiest day be received by these highly educated, woke classmates?
Of course, they will be polite, they will be no less polite about Philistine political opinions as are those who populate faculty lounges. But what of the atmospherics behind the politeness?

I just played this scenario in Ultimate General: Civil War. It is, of course, the Antietam scenario.
I not only defended all my victory points, but actually counterattacked after the Union spent themselves on my defenses. I believe I eliminated more than 90% of their army.
In the real world, this would have been equivalent to the Union Gettysburg victory and would have marked the end of the war, with the Confederacy victorious.
In this situation, I expect that by the 1920s, the issue of slavery would have been on the ballot in the South, as machines were more economical than slaves.
Then, the Confederacy and the Union would have fought together in WWII against the Axis forces.
After that, maybe in the 1950's or 1960's, I could see the Confederacy joining the Union either loosely, or more formally, reuniting our countries... and this time, the concept of successful secession would have been proven as viable, so the North might have minded its p's and q's a little better this time.
I am wondering if this successful escape from a near disaster in 1862 did not contribute to Lee’s over confidence 9 months later some 25 miles to the north at Gettysburg? Memories are a powerful influence upon future actions and this was a perfect example of how the CSA Army of Northern Virginia out thought the USA Army of the Potomac, even at the 1 to 2 odds of available forces.
Of course there were significant differences between the two forces at Gettysburg and Lee erred in mis-accounting for them. Longstreet was not Jackson and Meade was not as pathetic as McClellan. Also significant was how the Union (USA) forces were learning how to fight and to RESPECT Lee’s strategy & tactics. Also, the US Navy was successfully implementing the strangle plan that reduces the CSA’s support. They took the Port Royal Sound / Hilton Head in 1861 and then New Orleans in 1862 from the sea.
If Jackson had not been killed in May, it is a near certainty that he would have been Lee’s ‘Second in Command’ and the endless speculation of what would the advice be on that 3rd day? The charge to the center (Pickett) OR move the ~70 miles towards Washington for better ground? The CSA, in both these campaigns was seeking a “Battle of Saratoga” victory that MIGHT get a foreign alliance and aid for the Confederacy.