Halford, who scored the winner in Pompey's last away win in the league at Hull in October, ghosted into the Swansea penalty area unmarked in the 65th minute to finish off a neat set-piece move that had caught the home defence flat-footed.
Swansea, who missed the chance to move level on points with Welsh rivals Cardiff, had raced into an early lead when Craig Beattie scored his first goal of the season in the second minute.
But Pompey, who had taken only one point from their last four league matches, grew in stature as the first half wore on and David Nugent fired an equaliser two minutes before half-time.
Swansea skipper Alan Tate returned from suspension and Scott Sinclair was recalled, while Portsmouth were unchanged from the side which lost at Barnsley last weekend.
The Swans required a stoppage-time goal to snatch a point at Doncaster in their last match, but wasted little time forging an early lead after a trademark move down the right.
Mark Gower supplied the cross and Beattie fired home a low finish into the bottom corner from 12 yards in the second minute.
Portsmouth's response was almost immediate as Dave Kitson forced home goalkeeper Dorus De Vries to turn away his goalbound shot at full stretch two minutes later.
Nathan Dyer squandered another golden chance for Swansea in the 27th minute and Beattie headed wide in the 38th, but Portsmouth came more into the game as the first half progressed and grabbed an equaliser two minutes before the break.
John Utaka's fine through-ball opened up the home defence, sending Nugent racing clear on goal and the Pompey striker finished in clinical fashion from the edge of the area.
Beattie headed another cross from Gower wide early in the second period, but Swansea found the visitors in determined mood, with Liam Lawrence pulling the strings in the Portsmouth midfield.
Pompey, who had lost six times on their travels in the league this season, turned the game on its head in the 65th minute when Halford raced on to the end of a cleverly-worked free-kick involving Utaka and Lawrence to head home the latter's weighted cross into the bottom corner from six yards.
Swansea went straight on the offensive and forced a series of corners from which Ashley Williams headed over the crossbar.
Swansea manager Brendan Rodgers sent on Stephen Dobbie and Kemy Agustien for Gower and Albert Serran with 15 minutes remaining, but Aaron Mokoena headed narrowly off target for the visitors, who sensed an unlikely win was in their grasp.
Agustien's thumping drive whistled over the crossbar in the 79th minute and Dobbie fired straight at Ashdown soon after, but Pompey held firm for their first league win since a 2-1 home victory over Nottingham Forest at the end of October.