The fifth seed was in real danger of exiting the competition before eventually bouncing back to take the match 6-7(5) 6-4 6-4 in a little under two hours.
Both players lost serve on two occasions in the opening set, paving the way for a tie-break which Keys managed to take with her fourth set point.
A break in the opening game of the second set put the American on course for a big victory, but that appeared to spark Halep into life as she held for the remainder of the match.
Keys had less success in that department, though, seeing her hopes fade away with two losses of serve in the second set and another in the sixth game of the deciding set proving enough for Halep to claim victory.
Halep, a semi-finalist here two years ago, will now face Angelique Kerber in the next round.