Harley burst through a gaping hole next to the left-hand post after the Samoans had looked like justifying their position one spot above their 10th-placed opponents in the IRB World Rankings.
Greig Laidlaw's straightforward conversion handed the visitors a win that seemed unlikely when they trailed 16-10 and Sean Lamont knocked on with time almost up.
But the Scots seem to have found something that evaded them for seven straight games this season - a winning habit - as they backed up last week's success against Fiji and their opening tour upset of Australia.
A try for Joe Ansbro, who was a late call up to the starting XV after Nick De Luca failed a fitness test, gave Andy Robinson's men a 7-6 half-time lead but they looked to be in real trouble when Samoa fly-half Tusi Pisi converted his own try for a 16-point personal haul.
Somehow Scotland kept their composure, though - just as they had done against the Wallabies earlier in the month - and they return home unbeaten after a three-match tour that has seen them travel to three different countries in as many weeks.