Hafner's clout in the fourth tied the score 3-3, but the Blue Jays went back in front in the sixth on Brett Lawrie's solo shot.
That brought up Hafner, who blasted a pitch to the wall in center that glanced off the glove of Rajai Davis for a decisive three-bagger.
The rally made a winner of CC Sabathia (4-2), who surrendered four runs (three earned) on nine hits over eight innings. The left-hander didn't issue a walk and struck out four.
Joba Chamberlain notched his first save since 2010 despite allowing a pair of hits in the ninth as the Yanks notched their third straight against Toronto.
Jose Bautista cracked his seventh homer of the season for the Blue Jays.