Wrexham will play at Wembley for the first time in their history after reaching the FA Trophy final.
3-1 from semi-final first leg,
Danny Wright's 20-yard strike extended Wrexham's advantage after 22 minutes.
Terry Hawkridge quickly responded to raise Gainsborough hopes, and Michael Leary's deflected goal six minutes from time set up a frantic finish.
Joe Clarke headed off the line in the sixth-minute of injury time as Wrexham repelled heavy pressure to progress.
They will meet Grimsby Town, promotion rivals in the Blue Square Bet Premier, in the final on Sunday, 24 March.
Gainsborough, who play in a division below, threw everything in search of a third goal that would have taken an all-action second leg into extra time.
Four Welsh clubs at Wembley in 13 months
- Cardiff City - League Cup final, 26 February, 2012 (lost on penalties to Liverpool)
- Newport County - FA Trophy final, 12 May, 2012 (lost 0-2 to York City)
- Swansea City - League Cup final, 24 February, 2013
- Wrexham - FA Trophy final, 24 March, 2013
Wrexham goalkeeper Chris Maxwell was subjected to an aerial onslaught from a series of corners and crosses in the dying stages, and was indebted to Clarke for clearing a header off the line.
It all started so differently for the visitors as Wright opened the scoring in spectacular style.
The striker, who also scored the opener in the first leg, brought down a long clearance from Maxwell, beat a couple of defenders and found the top corner from outside the penalty area.
That left Gainsborough - conquerors of three Conference Premier sides on route to the semi-final - needing to score four goals to win, but they wasted little time in giving themselves a chance.
Having equalised after eight minutes in the first leg, they needed half that time to respond on this occasion as Hawkridge's shot from 12 yards went in off the post.
That was the cue for Gainsborough to increase the pressure, and they ruffled the visitors either side of half-time.
They were denied a second by a superb save from Maxwell, diving low to his left after the ball was diverted towards goal off a Wrexham leg.
With the storm weathered, Wrexham went in search of the second goal to ensure a more comfortable passage to Wembley.
Inviting chances fell to Clarke and Hunt, and the visitors were made to pay for their wastefulness as Gainsborough cut the deficit to one goal on 84 minutes.
Leary deflected shot gave Maxwell no hope, and Gainsborough sensed an upset.
A lengthy injury stoppage gave them seven minutes of added time, but a combination of desperate defending and a slice of luck saw Wrexham home as they became the fourth Welsh club to reach Wembley in the space of 13 months.
View from the dressing room:
Wrexham player-manager Andy Morrell told BBC Radio Wales:
"I am happy but a bit subdued because we've lost the game and our unbeaten run has come to an end.
"We knew they were going to throw everything at us. I thought getting the first goal would be key and I thought we were going to be comfortable after we got it.
"That wasn't to be, they scored and it changed the game. The first goal wasn't a throw in and the second was a foul on Wrighty (Stephen Wright), but that aside, we're through to Wembley and that's what we've got to look forward to."
Budtz, Roma, Wilde, Waterfall, Danny Hone, Leary, Russell, Nelthorpe, Yates, Stamp, Hawkridge.
Subs: Connor, Barraclough, Clarke, Thompson, Young.
Maxwell, S. Wright, Riley, Westwood, Ashton, Harris, Keates, Clarke, Hunt, D. Wright, Morrell.
Subs: Coughlin, Cieslewicz, Ormerod, Little, Walker.
Darren Bond (Lancashire)