The Staggies went in front through Brian Graham before Sean McLoughlin equalised for Saints just before half-time.
St Mirren dominated the second half but left it late to get the winner with Foley firing his shot into the top corner two minutes from time.
Manager Jim Goodwin had made three changes from the team beaten at Tynecastle. Out went the injured Kirk Broadfoot with Stephen McGinn and Tony Andreu dropping to the bench. In came Gary MacKenzie, Ryan Flynn and Ilkay Durmus.
County gave a debut in goal to Chelsea loanee Nathan Baxter as one of three changes. Joining him in the starting line-up were Ross Stewart and Billy McKay, with Ross Laidlaw, Michael Gardyne and Lee Erwin all making way.
Saints had the best chance of the first half, a flashing shot from Durmus that went wide of the target.
The Turkish forward then turned provider, sending in a corner that fell to Foley whose shot was blocked. From another St Mirren corner, Kyle Magennis blazed a volley over the top.
County were almost caught out when Jon Obika sprung free for the home side. The Englishman made room for a shot but could not keep it on target.
Saints paid for their wastefulness after 24 minutes when County scored with their first shot on target. It was a clinical finish from Graham who converted from around 12 yards out.
McKay could have made it two just minutes later only for Vaclav Hladky to deny him with a fingertip save.
Saints responded with an effort from Danny Mullen that went well over the top, but they finally got a shot on target after 43 minutes for an equalising goal.
Ryan Flynn went down looking for a penalty but, before referee Euan Anderson could blow, McLoughlin thrashed the loose ball into the net.
That gave the home team a lift and they started the second half in positive fashion. But Obika's left-foot shot was too weak to beat Baxter before a Durmus attempt was deflected for a corner.
St Mirren should have moved in front after 73 minutes. Flynn did brilliantly to tee up Durmus but he completely fluffed his shot in front of goal.
That looked to be a costly miss until Foley took a pass from Andreu and thumped it into the top corner.