Maria Sharapova dug deep to all-but secure her place in the last four of the WTA Championships in Istanbul.