Striker Isaiah Rankin has been an outcast at Oakwell for long periods this season but he came off the bench to grab the most important goal of Barnsley's season.
With the other struggling sides all winning it was imperative that Barnsley also bagged the three points which would ensure their Second Division survival.
They battered a below-par Brentford side for long periods but squandered a hatful of chances and it looked like being a goalless draw until Rankin struck two minutes into stoppage time.
Fellow substitute Rory Fallon challenged for a long ball from Chris Morgan and it fell kindly for Rankin on the edge of the box and he smashed in a terrific right-foot shot that gave keeper Alan Julian no chance.
The victory was a relief for Barnsley boss Glyn Hodges who said: "It has been an emotional day.
"It has also been a very difficult season, but at least it ended on an enjoyable note.
"It was the story of our season all wrapped up in 90 minutes. We dominated the game and created numerous chances but a lot of goals went begging.
"We have a number of young players who have established themselves and hopefully that will be the way we go forward."Barnsley dominated the first half but Kevin Betsy, given a striking role in the absence of Bruce Dyer, passed up on three golden opportunities to have wrapped the game up inside 20 minutes.
Mike Sheron also ballooned two openings over the bar while a 26th minute volley from midfielder Gary Jones brought a fine save from Julian.
The chances continued to come and Julian again had to deal with a shot from Jones after Betsy had wriggled his way clear down the right.
Brentford defender Ibrahim Sonko then cleared off the line after Betsy had prodded his shot through Julian's legs from 12 yards.
The second half continued in the same vein and Julian again came to his side's rescue with an excellent one-handed save to deny Betsy.
There was an unbelievable let-off for the visitors just after the hour when a low cross from substitute Dean Gorre rolled untouched across the six-yard box, with two Barnsley players failing to get a finishing touch.
The home side continued to pour forward, with Hodges sending on strikers Rankin and Fallon, and it was Rankin, expected to leave the club in the summer, who popped up with a life-saving goal shortly after the fourth official had lifted the board indicating six minutes of stoppage time.