Saints scored after just 30 seconds through Steven Thompson, but the hosts levelled two minutes later courtesy of Callum Paterson and gained the upper hand in the 52nd minute when Hamill converted from 12 yards after Marc McAusland had been shown a straight red card for fouling Dale Carrick.
Despite going a man down, the Buddies continued to threaten and Thompson passed up two more great chances in front of goal but they could not find an equaliser.
The win moved bottom side Hearts within 16 points of Partick ahead of Sunday's crucial League Cup semi-final against Inverness.
The teams traded goals within the first three minutes in a frenetic start to the game.
Just 30 seconds were on the clock when Thompson headed Saints into the lead. The visitors retained possession from kick-off and worked space for Jason Naismith to deliver a cross from the right, and Thompson rose highest in the area to head the ball into corner of Jamie MacDonald's goal.
Hearts were left stunned but regrouped to repel further attacks before levelling just two minutes after Thompson's opener. Sam Nicholson found David Smith on the flank and the winger's deflected cross was nodded into the net by Paterson from close range.
The fingertips of Saints goalkeeper Marian Kello then prevented Hearts from going ahead in the 13th minute. The tricky Nicholson did well to make room for a cross from the left, but this time former Hearts favourite Kello acrobatically tipped Paterson's header over the bar.
Back came St Mirren, though, to cause further problems of their own. Adam Campbell's cross was miscued by Paul McGowan at the back post and Hearts defender Dylan McGowan swept the loose ball clear before Thompson could pounce.
Hearts then enjoyed a spell of pressure and Carrick's first-time left-footed shot from a low Paterson pass was deflected over Kello's bar by Goodwin.
At the other end, goalkeeper MacDonald needed two attempts to collect a low McGowan drive.
Saints hit the bar 10 minutes before the interval with a diving header from McAusland following a John McGinn corner.
The away side passed up another opportunity to score three minutes before the break. McGowan found Campbell inside the area and, when the on-loan Newcastle forward's shot was blocked by MacDonald, Conor Newton headed the rebound wide.
The contest swung firmly in Hearts' favour after the restart when they were awarded a penalty and visiting defender McAusland was ordered off.
Smith led a counter-attack charge before feeding Carrick, who was brought down by McAusland just inside the area. Hamill stepped up to take the spot-kick and sent Kello the wrong way with a low finish.
Despite being a man down, St Mirren continued to look dangerous as Hearts invited pressure by sitting deep.
MacDonald was called on to punch a 25-yard Goodwin free-kick clear, while Gregg Wylde's introduction added pace to the St Mirren attack and his cross was hit over by Thompson at the front post.
Hearts wasted a great chance to add a third in the 75th minute when Carrick slid in to meet a Brad McKay flick in the box but his effort came back off the post with only Kello to beat.
Wylde and Thompson combined again five minutes from time and the latter passed up another great chance from six yards, blasting a volley over.