The Commons voted by 307 to 301 against the amendment from pro-EU Tory rebels that would have wrecked the Prime Minister’s Brexit strategy and plunged the government into chaos.
Having faced down Remainers in her own party, Mrs May will now take the plan set out in last week’s White Paper to Brussels in a bid to salvage a Brexit deal.
Twelve Conservative MPs rebelled against the government to vote for a proposal that would have kept the UK in the customs union if no alternative plan for frictionless trade with the EU had been agreed by January 2019.
Defeat would likely have made a confidence vote in the government inevitable. Mrs May only survived the threat of a major rebellion by Brexiteer Tory MPs on Monday by accepting their amendments to another piece of trade legislation.
With Brussels expected to reject the government’s proposals in the White Paper when Brexit talks resume next week, speculation is mounting that the UK could face a Brussels ultimatum to choose between a Norway-style deal inside the single market, or leave the EU with no deal at all.
One pro-EU MP told the Scotsman that he now expected the Commons to be faced with just such a take-it-or-leave it decision in a vote later this year, and described the past two days as a “farce”.
Meanwhile, SNP MP Angus MacNeil appeared to put pressure on Nicola Sturgeon, tweeting that the vote “now ensures that Scotland’s SNP Government has no option but to hold #indyref2 in next year or so”, adding: “Big bad clouds have golden linings”.
Despite the government's victory, Labour’s Shadow International Trade Secretary Barry Gardiner said the knife-edge vote showed the Prime Minister was "in office, but not in power".
“The Government’s handling of Brexit over the past week has been an utter shambles," Mr Gardiner said.
“Each day that Ministers waste arguing among themselves increases the risk of the UK crashing out of Europe without an agreement."
Shadow Scotland Secretary Lesley Laird said the fact that no Scottish Conservative MPs rebelled against the government meant "Ruth Davidson’s pledge that her MPs would stand up for Scotland was nothing short of a lie."
“At every step they have been nothing short of lobby fodder for Theresa May and Ruth Davidson has done absolutely nothing about it," Ms Laird claimed.
“The Government have a majority of 13 and there are 13 Scottish Tories. The vote was lost by four.
"The fact that they have no backbone and are unwilling to do what’s in the best interests of the country tells you everything you need to know.”
The Green Party MP Caroline Lucas said the vote would strengthen calls for a second EU referendum on the terms of the UK's Brexit deal.
“Brexit has sunk further into the Westminster quagmire tonight with the Government losing a key vote on medicines and scraping home on a vote on the Customs Union.
“Parliament is deeply divided on Brexit and the Government are struggling to get anything passed.
“The only way forward is put this issue to the public and have a 'People's Vote' on the final Brexit deal.”
In the preceding vote, the government had fallen to only its second defeat on Brexit in the Commons, as MPs voted by 305 to 301 for continued UK involvement in the EU's regulatory system for medicines.
The amendment from Conservative MP Philip Lee, who resigned from the government in protest over Brexit, requires the government to seek continued UK participation in the EU medicines regulations involving the European Medicines Agency.