Fourth seed Halep suffered a third straight early exit in New York, having been knocked out in the opening round in 2017 and 2018.
Townsend, 23, equalled her best run at a Grand Slam after also reaching round three on her main draw debut at Roland Garros in 2014.
"This means a lot. It has been a long journey," Townsend said, wiping away tears.
Halep saved a pair of match points as Townsend served at 5-4 in the final set but the left-hander displayed impressive resolve in the ensuing tie-break to notch her first win over a top-10 player.
"I had a match point against (Kiki) Bertens at Wimbledon. I've had so many big matches," Townsend said, recalling an agonizing first-round defeat at the All England Club last month.
"This match means so much to me and gives me so much confidence to get over the hump and know that I can do it."
Townsend had lost all three previous meetings with Halep in straight sets, but credited a change in mindset for helping turn around her fortunes at Arthur Ashe Stadium.
"When I've played her before I was just trying to make balls. I was playing not to lose. I decided today I was playing to win," she said.