The Spanish world number 35 capitalised on an error-strewn Williams display in the second set to level the match and take it to a decider.
But the 37-year-old American ninth seed recovered to win six games in a row and came through 6-3 3-6 6-1.
She will now face Petra Kvitova after the Czech 13th seed beat Garbine Muguruza.
“The energy of the crowd here, it makes me feel I’ve got to hit a winner, I’ve got to hit an ace. I love it here,” Williams said in front of the Arthur Ashe Stadium crowd.
Williams has now progressed from the last 16 at Flushing Meadows 12 times since making her debut in 1997.
She has won the title on two occasions, beating Lindsay Davenport in 2000 and her younger sister Serena the following year.
“I don’t know how to explain my longevity,” said Williams. “If I did I would bottle it and sell it. My sister and my family motivate me a lot.
“I am focused on myself and being as aggressive as possible. Nobody gives you a Slam, you got to take it and I am trying to take it.”