Hayes curled home from 25 yards in the 69th minute at Greenhill Road on Saturday and Caley Thistle held on to jump three places and consign Aberdeen to bottom spot.
Inverness had dominated the first half after Hayes set up Andrew Shinnie for an 11th-minute opener but St Mirren striker Steven Thompson headed home in first-half injury time.
Hayes provided the one moment of real class after the break to net his second goal in four starts following his comeback from a shoulder injury.
The Irishman’s absence was a key factor in Caley Thistle’s period at the bottom but they can now look up after a solid performance.
St Mirren manager Danny Lennon had changed his tactics but the move failed to pay off, although the game could have been different had the recalled Thompson converted a simple early chance.
The hosts lined up with a 3-5-2 formation with Jim Goodwin in the centre of defence and Gary Teale at right wing-back.
Inverness were pressing them high up the park with their 4-2-3-1 formation but the hosts had the early chances in wet and windy conditions.
Nigel Hasselbaink got a couple of shots in early on, forcing a corner which should have led to the opener.
Thompson had time to take a touch after Lee Mair headed Teale’s delivery across goal but he then shot over from six yards.
Saints shot themselves in the foot three minutes later when Marc McAusland played a loose pass across his 18-yard box that was picked up by Hayes.
The Irishman could have shot himself but he played a composed square ball to Shinnie, who stroked home from 12 yards.
The goal was another personal disaster for McAusland, who gifted Celtic an early opener last weekend with a poor clearance.
St Mirren were struggling to recover from the blow and Inverness, who had Roman Golobart in for the injured Josh Meekings, continued to push forward.
Goodwin headed Richie Foran’s header off the line following Hayes’ corner and Greg Tansey curled in a dangerous cross towards Ross Tokely following a short free-kick but Thompson managed to glance the ball just past.
Lennon changed to a 4-4-2 formation around the half hour, moving Goodwin into midfield to shadow Shinnie, with Illias Haddad dropping to right-back and Teale pushing on.
Saints could not immediately turn the tide. Billy McKay, preferred to Gregory Tade up front for Inverness, got a shot in before Tansey drove just wide from 20 yards.
However, Teale put in some dangerous balls towards the end of the half and St Mirren levelled two minutes into injury time.
Esson was forced to push over Jeroen Tesselaar’s over-hit cross for a corner and Thompson sneaked round the back to head home Teale’s inswinging delivery.
David Barron replaced Mair at half-time with Haddad switching roles again to centre-back and both sides threatened in the early stages.
Samson dived to hold McKay’s header before Tesselaar set up Thompson 16 yards out, with Esson parrying his measured strike.
Inverness had a spell of pressure but Esson then produced a smart save to push over Teale’s 35-yard free-kick, which carried towards the top corner in the wind.
The game grew increasingly scrappy before Hayes lit it up. Tansey passed up the chance to shoot after a clever inside pass from Foran but worked the ball out to Hayes, who turned and curled the ball just inside the far post.
McKay almost capitalised on some confusion in the St Mirren defence seconds later as he beat Samson to a long ball, but he slipped and scrambled a shot just wide from the floor.
St Mirren brought on Graham Carey and Jon McShane and pushed for an equaliser but Caley Thistle defended their lead resolutely.
And Esson secured the victory in injury time when he got down well to hold Thompson’s glancing header from a long free-kick from Goodwin.