But Judge Robert Drain didn't accept it, the person said . He told Sears and ESL to give it another go. There was a chance of saving thousands of jobs and they should try to figure out how to do so – by midnight.

The roughly $5 billion offer Lampert was making through an affiliate of his hedge fund ESL Investments wasn't large enough to cover all of the company's administrative expenses, like vendor payments and advisory fees. Sears' unsecured creditors had virulently argued since day one against Lampert's efforts to revive the retailer. The offer relied on a $1.3 billion so-called credit bid, meaning the deal is funded in part by forgiving debt owed to ESL. Sears' unsecured creditors have objected to its use.

There were too many sticking points, according to the people, who spoke on condition of anonymity because the information is confidential.

Lawyers and bankers who had been holed up in the offices of Weil, Gotshal & Manges on Fifth Avenue to work out a deal between Lampert and Sears Holdings were resigned to the reality that the company would liquidate and the Sears chairman's efforts to save up to 50,000 jobs would have been for naught, people familiar with the situation told CNBC.

Sears and ESL's lawyers hunkered down again, as advisors ran through the offices meeting with Sears' restructuring committee and ESL.



But midnight came and went. By 2 a.m., after hours of gamesmanship and negotiations, Sears and ESL finally reached a deal. ESL agreed to boost its offer by about $150 million, putting the full bid slightly over $5.2 billion, the people said, and keeping the company from shutting about 400 stores.

Lampert found yet one more rabbit to pull out of a hat with a seemingly infinite capacity for bunnies.

Neither Sears nor ESL are out of the woods yet. Sears unsecured creditors are opposed to the bid, people familiar with situation said. They have said there may be claims against Sears for deals done under Lampert's tenure as CEO and its largest shareholder, which include Sears' spinoff of Lands' End in 2014 and transactions with Seritage Growth Properties, a real estate investment trust Lampert created through some Sears' properties a year later.

If the unsecured creditors formally object to the bid, the bankruptcy judge will need to assess the merits of their claim at a hearing Feb. 1 at the Southern District of New York Bankruptcy Court in White Plains. Lampert needs Drain's approval for the bid to be official.

Such lawsuits and pressure are not atypical of bankruptcies. In a situation as thorny as Sears', however, it is possible the unsecured creditors believe they can squeeze more out of Sears through litigation than through a sale to Lampert.

As part of the deal struck in the early hours, ESL is offering $35 million to settle those claims, the person said. It is likely the unsecured creditors will, at the very least, push for more settlement money from Lampert.

The unsecured creditors have eight days to object to a bid. Sears then has two days to offer up a response.

A spokesperson for ESL declined comment.

The owner of Kmart and Sears had filed for bankruptcy in October, and Lampert's bid was the only one that would have kept it alive. The initial offer started at about $4.4 billion, and then was sweetened to more than $5 billion through weeks of negotiation. It had strong bargaining tools: It could save up to 50,000 jobs and create an infrastructure to support the continued operations of its businesses like Sears Home Services.

Even if Sears ultimately emerges from bankruptcy, it will need to address the challenges that led to its initial downfall.

ESL has said it is buying only Sears' profitable stores, but, as a whole, the company hasn't turned a profit since 2010.

Sears operated about 700 stores and had more than $7 billion in assets when it filed for bankruptcy, making it the biggest of a series of retail bankruptcies in recent years. It has since shrunk store footprint to a little over 400 through multiple rounds of closures. It could not be immediately determined whether Lampert's bid will save all the remaining stores.

Lack of investment has left its stores largely neglected, and, as result, the retailer has lost its relevancy with customers. Sears is competing with well capitalized competitors like Walmart and Target, which have beaten Sears over the years by pouring money into private-label brands, while also offering goods at affordable prices. Those retailers are also investing in technology like delivery that is more difficult for a smaller, more constrained player like Sears.

Amazon, meantime, continues to dominate retail and threatens all chains with its strong membership program and investors that are willing to overlook margins.

An economic slowdown in 2019, which some analysts are expecting, could further challenge Sears. The retailer, whose home improvement products are closely tied with home building and refurbishing, was hurt immensely by the Great Recession.

Retailers that emerge from bankruptcy don't always stay out for long. While companies are negotiating with lenders and slimming their footprint to restore profitability, competitors are pouring money into strengthening their business. Children's clothing retailer Gymboree Group is expected to file for bankruptcy protection as soon as this week, CNBC has reported, in what would be its second bankruptcy filing in less than two years.

WATCH: Sears was the Amazon of the 1930s. Here's where the retailer is today