European Union leaders have granted the UK a six-month extension to Brexit, after late-night talks in Brussels.
The new deadline - 31 October - averts the prospect of the UK having to leave the EU without a deal on Friday, as MPs are still deadlocked over a deal.
European Council President Donald Tusk said his "message to British friends" was "please do not waste this time".
Theresa May, who had wanted a shorter delay, said the UK would still aim to leave the EU as soon as possible.
The UK must now hold European elections in May, or leave on 1 June without a deal.
The prime minister will later make a statement on the Brussels summit to the House of Commons, while talks with the Labour Party, aimed at reaching consensus on how to handle Brexit, are set to continue.
Mrs May tweeted: "The choices we now face are stark and the timetable is clear. So we must now press on at pace with our efforts to reach a consensus on a deal that is in the national interest."
So far, MPs have rejected the withdrawal agreement Mrs May reached with other European leaders last year and they have voted against leaving the EU without a deal.
The EU has ruled out any renegotiation of the withdrawal agreement.
Before the summit, Mrs May had told leaders she wanted to move the UK's exit date from this Friday to 30 June, with the option of leaving earlier if Parliament ratified her agreement.
What is the reaction in the UK?
For Labour, shadow Brexit secretary Sir Keir Starmer called the delay to 31 October "a good thing", saying businesses would be "relieved".
He added: "Negotiations are in good faith. We all feel a deep sense of duty to break the impasse.
"But there's also this question of how on Earth do we ensure that anything this prime minister promises is actually delivered in the future because of course she's already said she's going to step down, probably within months."
One government minister told BBC political editor Laura Kuenssberg the latest delay to Brexit could mean a Conservative Party leadership contest after Easter, with a new prime minister potentially in place by June.
Former Brexit Secretary David Davis said: "There's been no progress whatsoever, really."
He added that it was still "difficult to see how" Mrs May could get her deal with the EU through Parliament and said: "The pressure on her to go will increase dramatically now, I suspect."
Scotland's First Minister Nicola Sturgeon tweeted her "relief" that the UK wouldn't be "crashing out" on Friday, adding that "allowing people to decide if they still want to leave is now imperative".
What was agreed?
- A Brexit extension "only as long as necessary" and "no longer than 31 October" to allow for the ratification of the withdrawal agreement
- The UK "must hold the elections to the European Parliament" and if it fails to do this, the UK will leave on 1 June
- The European Council reiterates there can be no reopening of the withdrawal agreement negotiations
What was the EU's message?
Donald Tusk emerged from the talks - and a subsequent meeting with Mrs May - to address reporters at a news conference at 02:15 local time (01:15 BST).
"The course of action will be entirely in the UK's hands," he said. "They can still ratify the withdrawal agreement, in which case the extension can be terminated."
Mr Tusk said the UK could also rethink its strategy or choose to "cancel Brexit altogether".
He added: "Let me finish with a message to our British friends: This extension is as flexible as I expected, and a little bit shorter than I expected, but it's still enough to find the best possible solution. Please do not waste this time."
European Commission President Jean-Claude Juncker said: "There will probably be a European election in the UK - that might seem a bit odd, but rules are rules and we must respect European law and then we will see what happens."
What did Theresa May say?
Mrs May spoke at 02:45 local time (01:45 BST). She said that, although the delay extends until 31 October, the UK can leave before then if MPs pass her withdrawal deal.
"I know that there is huge frustration from many people that I had to request this extension," she said. "The UK should have left the EU by now and I sincerely regret the fact that I have not yet been able to persuade Parliament to approve a deal."
She added: "I do not pretend the next few weeks will be easy, or there is a simple way to break the deadlock in Parliament. But we have a duty as politicians to find a way to fulfil the democratic decision of the referendum, deliver Brexit and move our country forward. Nothing is more pressing or more vital."
The PM said the UK would "continue to hold full membership rights and obligations [of the EU]" during the delay.
Trick or treat? Halloween deadline is both
You couldn't quite make it up. The new Brexit deadline is, you guessed it, Halloween.
So to get all the terrible metaphors about horror shows, ghosts and ghouls out of the way right now, let's consider straight away some of the reasons why this decision is a treat in one sense, but could be a trick too.
A treat? First and most importantly, the EU has agreed to put the brakes on. We will not leave tomorrow without a deal.
The prime minister's acceptance that leaving the EU without a formal arrangement in place could be a disaster won out.
And there are quite a few potential tricks. This new October deadline might not solve very much at all.
This could, although I hate to say it, just make way for months of extra gridlock before the UK and the EU find themselves back here in a similar situation in the autumn.
How did the EU leaders decide?
The EU had been split over the length of delay to offer the UK, and by law its other 27 member states had to reach a unanimous decision.
Although other countries backed a longer delay, French President Emmanuel Macron pushed for a shorter extension. He called the 31 October deadline "a good solution".
Taoiseach Leo Varadkar, the Irish prime minister, said the extension gave the UK time "to come to a cross-party agreement".
Risk of no-deal postponed
Fudge and can-kicking are the EU-familiar words that spring to mind at the end of this Brexit summit.
After all the drama and speculation leading up to the meeting, effectively all that happened here is that the threat of a no-deal Brexit has been postponed for another six months.
Time enough for the EU to hold European parliamentary elections, choose a new president of the European Commission and pass a new budget - without EU leaders having to keep one eye at least on the day-to-day dramas in the House of Commons.
Despite EU leaders' rhetoric beforehand, they granted this extension without hearing a convincing plan of Brexit action from Theresa May.
In the summit conclusions there is no evidence of the punitive safeguards mooted to ensure the UK "behaves itself" - refraining from blocking EU decisions - as long as it remains a club member.
Yes, EU leaders worry about who might replace Theresa May as prime minister. Yes, they're concerned these six months could fly past with the UK as divided as ever but their message to the UK tonight was: "We've done our bit. Now you do yours. It's up to you. Please use the time well."