Williams has not won a tournament since defeating her sister Venus in the final of the 2017 Australian Open.
Williams, now ranked number ten in the world, has won a record 23 Grand Slam titles - one more than former record holder Steffi Graf.
Williams made a solid start to Sunday's contest in California as she raced into a 3-0 lead, dominating from the baseline with a string of powerful forehands.
"Before the match, I did not feel great, and then it just got worse with every second; extreme dizziness and extreme fatigue", Williams said in a statement.
"I don't really play that often, so when I do, I need to play well", she said.
After Kozlova claimed a 3-2 advantage in the first set, Halep won three consecutive games to go to set point at 5-3 on Kozlova's serve.
"I can't breath", stated Williams in her conversations with on-court medics, with her coach Patrick Mouratoglou encouraging her to quit the match before she finally conceded.
She is now ranked 10th in the world.
There were service breaks aplenty as Halep and Kozlova traded four on the way to the first-set tiebreaker and four more to open the second set.
Muguruza said it felt like a hollow victory.
"It was hard for me to close those sets, but I'm actually proud that I could do that", added the Romanian.
The result saw Muguruza set up a fourth-round meeting with Kiki Bertens, the seventh seed who overcame Konta 7-6 (12-10) 6-4.
The Romanian was one of six seeds to progress, with Svitolina joining her after a 7-5 6-4 win over Daria Gavrilova.