It appeared as if the hosts would have to settle for a point before captain Bartley rose highest two minutes from time to give Leeds their seventh win in nine league games at Elland Road.
Brentford’s top scorer Scott Hogan was denied a sixth goal in as many matches by the offside flag in an otherwise forgettable first half.
Leeds enjoyed the better of the second period but it looked as if the loss of 14-goal striker Chris Wood to injury would prove costly until Bartley stole all three points.
The opening 15 minutes passed by without incident, the first chance coming when former Brentford winger Stuart Dallas fired well over from Kemar Roofe’s cutback.
Brentford had hit form again with back-to-back wins after losing four games in a row and that showed as they threatened on the break.
Lasse Vibe saw a tame effort comfortably saved by Robert Green after being played in by Romaine Sawyers before the Leeds goalkeeper moved across his line to safely gather Hogan’s glancing header from Tom Field’s dangerous cross.
Nico Yannaris had a shot deflected wide at the end of another dangerous Brentford attack and it was not long before the home fans grew restless.
The Bees continued to do most of the running without truly stretching Green, who got well behind a long-range strike from Harlee Dean and then watched a Sawyers shot sail into the crowd.
Hogan had a close-range effort chalked off before Roofe’s shot was deflected wide as Leeds mounted a rare attack in the closing stages of the half.
John Egan headed the first opportunity of the second period well wide but Brentford were lucky to survive when Leeds right-back Luke Ayling smashed a great chance over after taking Dallas’ corner down on his chest.
Souleymane Doukara then slid an even better opportunity just wide after Dallas presented him with a one-on-one as Leeds enjoyed their best spell of the game.
The home side looked determined to make their second-half dominance count, Ayling going close again on the back of good work from Hadi Sacko.
Leeds penned Brentford in as the clock ticked down and they got their reward when Bartley headed home Dallas’ cross from the right.
There was still time for Brentford to find an equaliser and they went close when Egan’s effort hit the top of the bar.
But Leeds held on to sign off from Elland Road for the year with another crucial victory.