Willo Flood's 74th-minute free-kick - which came just after Danny Wilson had been sent off for a second bookable foul on Adam Rooney - looked destined to send the home side down, especially after Ryan Stevenson squandered a chance to level.
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 survived the prospect of being sent down by Hibernian on Sunday and, although the Dons dominated the opening period, there was again a determination and defiance about them at Tynecastle.
Dale Carrick, who opened the scoring against Hibs, dropped out with a dead leg while Aberdeen were strengthened by the return of Peter Pawlett and Andrew Considine as they reclaimed second spot from Motherwell on goal difference.
Hearts started brightly and might have threatened another early opener had Sam Nicholson's strike not hit team-mate 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.
And Aberdeen quickly got on top as the half settled into a pattern of the home side defending deep.
Niall McGinn had a powerful long-range strike pushed away and Aberdeen stepped up the pace midway through the half. Pawlett steered a first-time shot wide then managed to get the ball through for Rooney on the left side but Jamie MacDonald was out quickly to block.
Wilson saved the hosts with an excellent penalty-box tackle on Pawlett and McGinn fired just over the top corner after his own free-kick was blocked.
Hearts did threaten again before the break but Stevenson could not get enough on David Smith's free-kick and, although they had the ball in the net in the final seconds, Paul McCallum only earned a booking for the challenge that put the ball, and Aberdeen goalkeeper Jamie Langfield, over the line.
That was McCallum's last action before being replaced by Scott Robinson. Hearts stepped up a gear without immediately threatening Langfield, even when handed a temporary numerical advantage when Barry Robson took an unscheduled toilet break around the hour mark, pointing to his stomach as he sprinted past bemused manager Derek McInnes and down the tunnel.
Robson was back on when Ryan Jack set up Rooney with a determined burst, but the Irishman failed to get hold of his shot.
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 goalscorer made another crucial intervention moments later when his head diverted Robinson's well-struck volley over the bar.
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.
The hosts even pushed for a winner, although McGinn sliced wide when presented with a good chance to regain the lead.