Jamie Walker had put Hearts in front but McHattie walked before Niall McGinn levelled but McGhee was to spark wild celebrations at Tynecastle with his late intervention.
Aberdeen's Scott Vernon spurned a hat-trick of first-half chances, the first coming when he broke clear but could only shoot tamely at Hearts goalkeeper Jamie MacDonald.
The Dons striker was again bearing down on goal shortly after, but fired harmlessly wide from a perfect position.
Hearts' Walker (18) promptly showed him how it was done, cutting in from the left after combining with Jamie Hamill and firing past Dons goalkeeper Nicky Weaver from 16 yards.
The hapless Vernon was sprung clear of the home defence a third time but once more he could not find the composure to bring his side level.
Derek McInnes threw on Calvin Zola in place of Vernon and the Congolese was soon in the thick of the action as he was challenged by McHattie in the box, the disbelieving Hearts left-back being shown a straight red card by the referee.
McGinn kept his head to equalise from the penalty spot (68) and the visitors were again appealing for a spot-kick shortly after when Peter Pawlett was tackled by Walker just inside the box; all that was forthcoming in this instance was a yellow for the Aberdeen man.
But it was Hearts who were to find a second wind, Hamill going close, before Callum Tapping flighted in a deep free-kick, Weaver failed to collect and McGhee (88) nodded in the winner.
There was still time for Josh Magennis to draw a force stop from MacDonald in added time to deny the Dons a point.