With Scotland trailing 16-10 in the final game of their three-match southern hemisphere tour, incessant pressure finally told on the hosts' defence as Mike Blair sent the Glasgow Warriors flanker through a huge gap by the left-hand post.
Greig Laidlaw slotted the simple conversion to ensure a clean sweep for Andy Robinson's side, following previous wins over Australia and Fiji.
A try from Joe Ansbro, a late addition to the starting line-up after Nick De Luca failed a fitness test on a thigh strain, put Scotland 7-6 ahead at half-time but they trailed after Samoa fly-half Tusi Pisi crossed and converted to complete a 16-point personal haul. But a solitary lapse in the closing seconds of the 80 minutes let Harley in for the winning score.
Samoa were in complete charge of the opening exchanges, with full-back Faatoina Autagavaia twice coming close to a breakthrough before Tusi Pisi, the fly-half restored to the side for this match, slotted an early drop goal with a penalty there if he missed.
It was another four minutes before Scotland made their first serious visit to the Samoan half. Centre Matt Scott made the score with a superb solo break through traffic in the middle of the field, with scrum-half Chris Cusiter in support. Lock Richie Gray then had just enough power to get to the line where Ansbro was on hand to touch down.
Laidlaw slotted the touchline conversion, but Samoa were soon back in kicking range, Pisi cutting the Scots' advantage to one point.
Samoa got first blood in the second half with a second Pisi penalty, but Scotland did manage to regain the lead with Sean Lamont, the wing, earning a penalty with a typically rumbustious charge. Then Paul Williams provided the cutting edge round the blind side of a ruck again and found Pisi in support to score.
Scotland got lucky again when Tim Visser spilled the ball and wing Paul Perez picked up to race for the line, only for the touch judge to rule that Visser had edged into touch before dropping the ball.
Scotland turned down a kickable penalty and went for a scrum but failed to shift the mighty Samoan pack. A second and a third scrum followed before Scotland eventually let it out into the backs, only to run into a brick-wall home defence. When Sean Lamont knocked on a bouncing pass, it appeared their chance had gone, but Harley had the final word.