The takeover of Wrexham Football Club has been agreed, with contracts exchanged between administrators and local car dealer Neville Dickens.
Wrexham fans are determined to stay at their Racecourse ground
If creditors and shareholders agree, Wrexham can move out of administration, preserving their league status.
The League Two club have been in administration since 2004 and face a 3 June deadline to avoid expulsion.
Local MP Ian Lucas said few realised how close the town had come to the "horrific blow" of the club collapsing.
The news emerged just before Wrexham's home 1-1 draw with Oxford United on Saturday. Although that result ended the club's hopes of reaching the League Two play-offs, many supporters were probably more interested in off-field events.
The joint administrators David Acland and Steve Williams, from the firm Begbies Traynor, said they agreed the deal with Mr Dickens' consortium late on Friday.
A proposal document will be issued to all creditors and shareholders of the club and meetings will take place before the end of May at the Racecourse ground.
In a statement the administrators said the Wrexham deal had been "the most difficult football assignment we have undertaken to date by far".
"Whilst the champagne corks are still in the bottle, the hurdles have been eliminated one by one and we hope that the end is in sight," the said.
The administrators also paid tribute to Wrexham fans, who supported Mr Dickens' bid for the club, saying "the club hopes to see you next season at the Racecourse and out of administration".
The road to the takeover agreement has been a long one for the club which, facing debts of £4m, called in administrators in December 2004.
Wrexham administrator David Acland confirms the agreement
It was then docked 10 penalty points by the Football League and, as a consequence, relegated from League One in summer 2005.
There followed a long legal battle involving the club's former chairman Alex Hamilton, until last month the Court of Appeal finally ruled that the stadium should stay with the administrators who would then be able to sell.
Mr Lucas said: "It has been incredibly complex and difficult, this process, and I've been really impressed that the supporters and the people at the club have been so patient in working our way through what has been a very difficult situation.
"I don't think we realise how close this club came to folding and that would have been a horrific blow for the town."
Mr Dickens maintained his interest in buying the club from the start of the troubles and once the legal wrangles were over his consortium signed an exclusivity deal with the administrators.
That deal had been due to expire on Sunday.
Earlier this month Mr Dickens revealed he had applied for planning permission to develop part of the Racecourse ground.
He said the consortium planned a new stand, along with private apartments and student accommodation and any profits would be ploughed back into the club.
As part of the proposals, Mr Dickens said he would demolish his own showroom, next to the ground.
Throughout the campaign Wrexham Supporters' Trust had backed the bid, gone on marches and agreed to pay £3,000 each month to assist the club's wages bill.