Halep was in the unusual position of going into the match as the No 1 seed but also the underdog against a player she had beaten just once in nine previous encounters.
The Romanian, who saw off Venus Williams in the previous round, dug in superbly after losing a one-sided opening set to force a decider, and might well have come out on top had she broken for 4-2.
But Williams saved three break points, broke Halep in the following game and served out a 6-1 4-6 6-4 victory to set up a last-eight meeting with Karolina Pliskova.
Williams said of Halep’s comeback: “That’s why she’s No 1. She literally lifted her game to a new level. I didn’t.
“But it’s a part of this journey on my way back. It’s 10 months, so I can’t be too upset at myself.
“So I’m still learning, which I think is, at my age and this point in my career, admirable and exciting.
“I have to, fight for titles. I have to fight for matches. Actually, I don’t have any titles yet, but just fighting for matches is really important. I can definitely go to a new level. I have to if I want to stay in the tournament.”
Williams now takes on seventh seed Pliskova, who has been under the radar so far but produced a masterful display to beat Garbine Muguruza 6-3 6-1, hitting 23 winners and only three unforced errors.
The Czech beat Williams in the US Open semi-finals in 2016 but lost in the quarter-finals last summer.
“I didn’t play well that match, so for sure it would be good revenge to play her again,” said Pliskova. “Different conditions here, I think I’m playing a little bit better than I was last year, so I’m looking forward to it.”
US Open champion Naomi Osaka recovered from a set down for the second consecutive match, defeating Anastasija Sevastova 4-6 6-3 6-4 to reach her first quarter-final at the event.
In the last eight, Osaka will play sixth seed Elina Svitolina, who saved five break points in the third game of the deciding set on her way to a 6-2 1-6 6-1 victory over Madison Keys.
In the men’s draw, Novak Djokovic admitted he was struggling physically after surviving a gruelling examination from Daniil Medvedev to reach the last eight.
The six-time champion has now reached the quarter-finals at least 10 times at all four grand slams thanks to a 6-4 6-7 (5) 6-2 6-3 victory over his 22-year-old opponent lasting three hours and 15 minutes.
“I didn’t feel so great in the last 20 minutes of the match or so,” said Djokovic afterwards. “But I’m confident I can recover and be ready for next one.”
In the next match, Djokovic will face a man who has spent even more time on court in eighth seed Kei Nishikori, who needed five hours and five minutes to see off Pablo Carreno Busta, coming from two sets down to win 6-7 (8) 4-6 7-6 (4) 6-4 7-6 (8).
Fourth seed Alexander Zverev suffered his latest grand slam disappointment with a loss to Milos Raonic.
The 21-year-old beat Federer and Djokovic back-to-back to win his biggest title at the ATP Finals in November, but he was unable to improve his record of having reached only one slam quarter-final as he suffered a heavy 6-1 6-1 7-6 (5) loss to the Canadian.
There was no doubt about Zverev’s commitment to smashing his racket, eventually flinging the mangled mess of metal and string to the court.
“It made me feel better,” he said. “I was very angry.”
In the last eight, Raonic, who began the tournament by beating Nick Kyrgios and Stan Wawrinka, will take on France’s Lucas Pouille after he reached his first slam quarter-final since 2016 with a 6-7 (4) 6-4 7-5 7-6 (2) victory over 11th seed Borna Coric.