The Bees' dreams of a top-two finish looked slim thanks to strikes from Pompey pair David Connolly and Shaun Cooper until 22-goal striker Donaldson struck with four minutes remaining.
His late winner came after Bradley Wright-Phillips had scored either side of the break in a hard-fought battle.
Wright-Phillips broke the deadlock after 21 minutes when he lashed home a half-clearance from 12 yards.
But the visitors responded within five minutes, Connolly curling home a beauty from the edge of the box before Cooper gave them a shock lead with a skidding long-range drive that sneaked in at the far post.
The Bees, who ensured a play-off spot with the win, rode their luck against Pompey's packed seven-man rearguard.
Connolly lifted a shot over the crossbar after Jed Wallace's low drive was only parried by Bees goalkeeper Simon Moore as the Londoners repeatedly struggled to make the breakthrough.
Brentford boss Uwe Rosler threw on a third striker in Marcello Trotta in the 74th minute and his deft touch teed-up Wright-Phillips to slide home after a goalmouth melee.
The Bees shattered Pompey's spirited resistance in the dying minutes when Donaldson latched on to a slide-rule Wright-Phillips through ball to chip his shot over goalkeeper Simon Eastwood in a dramatic finale.
- David Connolly
- Shaun Cooper