The Democratic leadership agreed to back the bill after accepting promises from Republicans for a debate later on the future of young illegal immigrants.
President Donald Trump, who signed the bill on Monday evening, took a swipe at Democrats.
It is the fourth temporary measure since October because Capitol Hill cannot agree a longer-term budget.
The spending bill passed the Senate by 81-18 and the House of Representatives by 266-150 earlier on Monday.
The so-called continuing resolution keeps the government funded until 8 February in the hope that Congress can reach a longer-term budget agreement in the meantime.
Thousands of federal employees who had been placed on temporary, unpaid leave since Saturday breathed a sigh of relief.
“It was essentially a lunch break,” Tom Chapel, a scientist at the Centers for Disease Control and Prevention in Atlanta, Georgia, told Reuters news agency.
Republican Senate leader Mitch McConnell said his party had “come to an arrangement” to negotiate on the Democrats’ calls for an immigration deal.
Democrats want protections from deportation for so-called Dreamers, more than 700,000 young immigrants brought to the US as children.
But Republicans had insisted no agreement was possible while federal government services were closed.
President Trump, a Republican, said in a statement: “I am pleased that Democrats in Congress have come to their senses.
“We will make a long-term deal on immigration if, and only if, it is good for our country.”
Democrats voiced scepticism of Mr McConnell’s promise and some liberal groups are infuriated by the agreement to reopen the government.
Possible Democratic 2020 presidential candidates in the Senate – Elizabeth Warren, Kirsten Gillibrand, Cory Booker, Bernie Sanders and Kamala Harris – all voted against Monday’s bill.
Senator Harris, of California, said it would be “foolhardy” to trust Mr McConnell’s promise to take up an immigration bill in the coming weeks.
Another California senator, Dianne Feinstein, told The Hill, a political news outlet, she was “very disappointed” in the deal because there’s no guarantee that Republicans would help Dreamers.
Democratic congressman Luis Gutierrez slammed his own party’s senators after the vote saying: “They caved. They blinked. That’s what they do.”
On Twitter, “Democrats CAVED” was trending on Monday evening.
Senate Democratic leader Chuck Schumer accused President Trump of failing to help reach a deal.
“The great deal-making president sat on the sidelines,” he said.
But many have expressed dissatisfaction with Mr Schumer’s handling of the shutdown, which began on midnight on Friday.
“It’s Schumer’s job to lead and keep his caucus together to fight for progressive values, and he didn’t do it,” said Ezra Levin, co-executive director of the activist group Indivisible.
Stephanie Taylor, of the Progressive Change Campaign Committee, said: “Today’s cave by Senate Democrats – led by weak-kneed, right-of-centre Democrats – is why people don’t believe the Democratic Party stands for anything.” (Courtesy BBC)