Willo Flood's second half free-kick - which came just after Danny Wilson had been sent off for a second bookable foul on Adam Rooney - looked set to send their hosts down.
But Hamill netted three minutes from time after Shaleum Logan had brought down Callum Paterson to edge Hearts into a double-figures points tally - 18 behind St Mirren with six games left.
Hearts had already survived relegation on Sunday and, although the Dons dominated the opening period on Wednesday night, there was again a determination and defiance about them in front of their faithful fans.
Hearts started brightly and might have threatened another early opener had Sam Nicholson's strike not hit team-mate Ryan Stevenson in the goalmouth following a corner.
But the home crowd were far more subdued than Sunday's dramatic win, perhaps affected by speculation over the club's chances of avoiding liquidation amid delays and uncertainty over the ratification of a rescue deal in Lithuania.
Aberdeen stepped up the pace with Niall McGinn and Peter Pawlett going close with long-range shots.
Hearts had a bizarre boost when handed a temporary numerical advantage when Barry Robson took an unscheduled toilet break on the hour mark, pointing to his stomach as he sprinted past bemused manager Derek McInnes and down the tunnel.
The hosts came close midway through the half when Paterson let fly from 25 yards, Langfield diving to his left to push the ball wide.
Robson was taken off in the 70th minute for Cammy Smith and Wilson was sent off three minutes later after pulling back Rooney.
Flood drove the free-kick from 25 yards but it looked like MacDonald had it covered. However, he did not get enough on his attempts to stop the ball and flew into the corner of the net.
The Hearts fans rallied in defiance but Stevenson missed a great chance to level with seven minutes left when he shot well wide following a short pass-back by Mark Reynolds.
However, Paterson beat Logan to a bouncing ball in the box and Hamill sent Langfield the wrong way from the resulting penalty before his celebration drew an angry reaction from Jack.