The US government shut down temporarily for the second time in less than a month after a senator mounted a protest over the budget.
Politicians had hoped to approve a funding plan before the midnight deadline (5am Friday UK time).
But Republican Senator Rand Paul raised concerns about US debt and repeatedly blocked a speedy vote on the bill, insisting the chamber vote on his amendment to set strict budget caps.
Just three weeks ago, a similar failure to agree on a new budget in January resulted in a US government shutdown for three days, with scores of federal agencies across America unable to continue operating.
"I didn't come up here to be part of somebody's club. I didn't come up here to be liked," Mr Paul said.
They say, "It's a binary choice, young man. Take it or leave it." Well, I'll leave it.
— Senator Rand Paul (@RandPaul) February 9, 2018
Without some type of funding bill, the failure to agree on a new budget means the US government has technically ran out of money.
Mr Paul later tweeted: "Tonight, you could feel the frustration and embarrassment growing in Congress as we exposed the hypocrisy of Republicans who are joining in an unholy alliance and spending free-for-all with Democrats at the expense of the American people and our party's supposed principles."
He added: "Make no mistake, I will always stand up for fiscal responsibility, regardless of which party is in power, and I will continue to call the Republican Party home to the ideas that led to Americans trusting us with government in the first place."
Two hours after the setback, congressional leaders scrambled to end the standoff in the middle of the night.
Senators eventually voted 71-28 to approve the $400bn (£286bn) budget deal, with the bill moving to the House of Representatives for a further vote.
The bill would hike defence and domestic spending by almost $300bn (£215bn) over the next two years.
With no plans for other spending cuts or new tax revenues, that additional spending would be financed by borrowed money – something both Democrats and Republicans in the House of Representatives voiced disapproval of.
Four hours after it was given the green light by senators, the bill was approved in the lower chamber too, by 240 to 186 votes.
US President Donald Trump signed the bill, ending the government shutdown.
He tweeted: "Just signed Bill. Our Military will now be stronger than ever before. We love and need our Military and gave them everything – and more. First time this has happened in a long time. Also means JOBS, JOBS, JOBS!"
More from US
"Without more Republicans in Congress, we were forced to increase spending on things we do not like or want in order to finally, after many years of depletion, take care of our Military. Sadly, we needed some Dem votes for passage. Must elect more Republicans in 2018 Election!"
The White House had earlier signalled its optimism of the shutdown being short, calling it a "lapse".