Leeds lost captain Liam Cooper to injury inside the opening quarter of an hour but United went ahead in the 27th minute when Tyler Roberts flicked home a Raphinha cross.
Brentford equalised nine minutes after the break as Shandon Baptiste converted from the middle of the box, shortly after United star Kalvin Phillips had pulled up injured and was eventually replaced by Mateusz Klich.
The Bees then bagged what looked set to prove the game's winning goal seven minutes later when a Bryan Mbeumo through ball played in Sergi Canos who smashed home a clinical finish past Illan Meslier.
But Bamford returned from two and a half months out through an ankle injury as United's third and final substitute in the 68th minute and incredibly kneed home a corner in the 95th minute with a finish that flew in off the underside of the bar.
After an early Whites attack which ended with Dan James being felled, Brentford began brightly and a Canos effort from the edge of the box was deflected into the hands of Meslier.
Mbeumo then fired well wide from a cross from the left but Cooper injured himself in closing the Brentford forward down and Jack Harrison was eventually brought on to replace the skipper in the 15th minute.
Mateusz Klich initially stripped off to come on but then sat back down and the introduction of Harrison saw James move upfront and Junior Firpo sent into a back three.
But Leeds went ahead 12 minutes later as a Raphinha cross from the right flew through the Bees area and was prodded home by Roberts on his 100th outing for the club.
As part of what was becoming an eventful afternoon for the Wales international, Roberts was then booked two minutes later for catching Mbeumo with a late challenge.
But Roberts almost doubled United's lead two minutes later as a fine rising shot from the edge of the box was tipped over by keeper Alvaro Fernandez for a corner which the Bees narrowly survived after a scramble at the far post.
A dangerous Roberts cross was then crucially cleared by Bees captain Pontus Jansson on his Elland Road return and shortly afterwards Roberts threatened again as a powerful cross/shot flew through the box.
Leeds were well on top and the half ended with Kalvin Phillips firing an effort over from 20 yards out before Charlie Goode was booked for going through the back of Roberts.
The Whites almost doubled their lead four minutes after the restart as a Luke Ayling header from a Raphinha cross was flying into the top left corner until Fernandez saved.
But Brentford equalised in the 54th minute when a Canos cross from the left was deflected into the path of Shandon Baptiste who fired home a low finish into the bottom left corner of the net.
Moments earlier, Phillips had gone down injured and the England ace had to be replaced by Klich as United were dealt a second injury blow.
A fierce Canos shot then struck Ayling in the face and Canos squandered a fine chance moments later when sending a free header wide following a cross from the right.
Yet Canos made no mistake one minute later as he latched on to a through ball from Mbeumo and converted a clinical finish past Meslier into the top right corner.
Patrick Bamford then returned from two and a half months out when coming on as United's third and final change when replacing Firpo in the 68th minute.
Dallas threatened eight minutes later after cutting in from the left but his curling shot was saved by Fernandez.
From a Raphinha's pulled back free-kick, a drive from Klich was then blocked by Goode before Roberts fired straight at Fernandez.
Leeds continued to push looking for an equaliser but Raphinha sliced a shot from distance well wide and Brentford looked destined to leave with a victory heading into the fifth and final minute of second half stoppage time.
But a late corner gave the Whites one last chance and the ball flew home off Bamford's knee in the 95th minute following Ayling's flick on.
The Whites are now six points clear of the bottom three and 14th after 15 games played although third-bottom Burnley have a game in hand.
Leeds United: Meslier, Ayling, Cooper (Harrison 15), Llorente, Firpo (Bamford 68), Phillips (Klich 54), Forshaw, Dallas, Raphinha, James, Roberts. Sub not used: Klaesson, Shackleton, Cresswell, Jenkins, Summerville, Rodrigo.
Brentford: Fernández; Goode, Jansson, Pinnock; Roerslev, Baptiste (Onyeka 69), Janelt (Jensen 90), Nørgaard, Henry; Mbeumo, Canós (Wissa 72). Subs not used: Cox, Thompson, Forss, Ghoddos, Stevens, Young-Coombes.
Referee: David Coote.
A message from the Editor:
Leeds has a fantastic story to tell - and the Yorkshire Evening Post has been rooted firmly at the heart of telling the stories of our city since 1890.
We believe in ourselves and hope you believe in us too. We need your support to help ensure we can continue to be at the heart of life in Leeds.
Subscribe to our website and enjoy unlimited access to local news and information online and on our app.
With a digital subscription, you can read more than 5 articles, see fewer ads, enjoy faster load times, and get access to exclusive newsletters and content.
Click here to subscribe.
For more details on our newspaper subscription offers click here.
Thank you Laura Collins