Lee Gregory struck a 98th-minute penalty winner as 10-man Millwall booked a League One Play-Off semi-final clash with Bradford City following a gutsy win at Priestfield Stadium.
Aiden O'Brien had handed The Lions a 55th-minute lead and the visitors were comfortably in control until the harsh 86th-minute dismissal of Mahlon Romeo, shown a straight red card for a hard challenge on Brennan Dickenson.
Boss Neil Harris was sent to the stands for venting his frustrations and when John Egan hauled Gills level in the second minute of injury-time it seemed as if Millwall were going to have to wait once more to end a 10-game winless run at the Priestfield Stadium.
However in the dying embers of the match sub Gregory was wrestled to the ground - ironically by Dickinson - and up he stepped cool as you like, to lash home a spot-kick and spark wild celebrations in the sunshine.
Whilst it was a long over-due victory for Millwall against their Kent rivals, it was tarnished by the heartbreaking sight of young right-back Romeo, such a pivotal figure in the team, leaving the field with his shirt pulled up high wiping away tears of despair that he could, pending an appeal, now miss the Play-Offs.
The game also proved to be devastating for Gillingham, whose dying fading hopes of gate-crashing the top in which they had spent virtually the entire season, were crushed.
Harris stuck with the same team that brushed aside Oldham Athletic last time out, but did welcome Shaun Williams and top scorer Gregory back from suspension and injury respectively. The duo took their place on the bench with Jamie Philpot and Kris Twardek making way.
After an opening period big on noise and effort, but with the football played at a frantic pace, the teams began to settle into their stride and exchange scoring chances.
Gillingham's Deji Oshilaja connected with Bradley Dack's 11th-minute corner and powered a header over the bar from 10 yards and then former Gills defender Joe Martin so nearly silenced those taunting him in the Rainham End with a curling free-kick that looked to be heading into the top corner before a deflection took the ball behind. Unfortunately the officials were unsighted and to Millwall's frustration, a goal kick was awarded.
Aiden O'Brien's 19th-minute angled drive was saved by Stuart Nelson in the home goal after Steve Morison's neat flick had played in his strike partner and Mark Beevers then had an effort cleared off the line and Martin's follow-up smacked the woodwork as The Lions began to prise and probe the Gills defence.
Oshilaja was allowed space to meet another Dack corner midway through the half, this time sending his header straight into the hands of Archer.
Thankfully Oshilaja's next connection wasn't quite so sweet, contriving to hit a first-time cross outrageously off target by volleying the ball over the heads of the bemused Lions fans packed behind the goal and out of the ground.
At the other end an intelligent pass from former Gill Mahlon Romeo picked out Chris Taylor, who had ghosted into the penalty area unmarked. Taylor laid the ball into the path of Morison, but the skipper's shot was blocked for a corner.
Edinburgh's side responded and League One Player of the Year Dack tried his luck from distance with a crisply struck 20-yarder four minutes before the break, but Archer was equal to the effort and saved smartly low to his right to ensure that the teams went in level at half-time.
The deadlock was broken 10 minutes after the restart when O'Brien slotted home from close range after Steve Morison's initial shot from a Taylor cross was parried by keeper Stuart Nelson.
Harris' men really had the bit btween their teeth now and produced some crisp football to frustrate a Gills side whose season disintegrated with every minute that went by.
It was so nearly 2-0 to Millwall 18 minutes from time when Taylor's corner from the right picked out Morison, whose looping back header hit the woodwork.
Seconds later play swung up the other end an underhit back pass from Romeo let in Norris, but he was denied by the quick thinking of keeper Archer who sprinted out of his goal and swept the ball clear.
Gillingham threw caution to the wind and adopted a shoot on sight policy with Dack, Josh Wright and Dack again peppering the visiting goal.
But The Lions were controlled and composed, keeping their shape superbly until life was made that whole bit harder when ref Paul Tierney stunned the Millwall camp by brandishing a red card to the distraught Romeo.
On came Tony Craig for Morison and The Lions were forced to defend as the home side threw everything at them in a bid to take advantage of their extra man.
With two of six minutes of injury-time showing, Egan thumped the ball home from close range to level up the score. But on an afternoon of high drama, Gregory broke into the penalty area and was unceremoniously hauled to the ground by Dickinson.
Showing nerves of steel the Millwall frontman made no mistake from the resulting penalty, giving Nelson no chance with his 25th goal of the season to put the seal on a near-perfect afternoon for The Lions.
Gillingham: Nelson, Egan, Ehmer, Loft (Williams, 58mins), Hessenthaler (Jackson, 58mins), McDonald, Samuel (Norris, 59mins), Dack, Dickensen, Wright, Oshilaja. Subs not used: Morris, El-Abd, Osadebe, Donnelly.
Booking: Dack (79mins)
Scorer: (Egan 90+2mins)
Archer, Romeo, Martin, Beevers, Webster, Thompson, Abdou, Ferguson (Upson, 71mins), O'Brien (Gregory, 74mins), Morison (Craig, 86mins), Taylor. Subs not used: Forde, Edwards, Williams, Onyedinma.
Archer (90+2mins), Taylor (90+5mins)
O'Brien (55mins), Gregory (90+8mins,pen)
9,375 (away 1,213)