WASHINGTON — Republicans vowing to govern effectively as a congressional majority failed a fundamental test Friday, when House leaders only narrowly managed to avert a partial shutdown of the Department of Homeland Security after an embarrassing defeat earlier in the day.

The seven-day funding extension, approved by a vote of 357 to 60, came just hours before money for the department was to run out at midnight. The accord was reached after a stunning and humiliating setback for Speaker John A. Boehner and his leadership team earlier Friday, when the House voted against their original plan to extend funding for the department for three weeks — a position that Mr. Boehner had considered a fail-safe. More than 50 House Republicans defected, voting against the bill.

The speaker was rescued by Democrats, who supported his offer of a weeklong extension because they believed it would lead to a vote next week on full funding for the department through the fiscal year, without any provisions related to President Obama’s executive actions on immigration included in the House’s original legislation. A spokesman for Mr. Boehner said the speaker had made no promises or deals with House Democrats to guarantee such a vote.

“Your vote tonight will assure that we will vote for full funding next week,” wrote Representative Nancy Pelosi of California, the Democratic leader, in a letter to her members on Friday night urging them to support Mr. Boehner’s seven-day funding measure. The House vote came after the Senate had already agreed to a seven-day funding extension.

About 10 minutes before the funding was set to expire, the White House announced that the president had signed the weeklong extension.

On Monday, the Senate will consider whether to enter into joint negotiations with the House over Mr. Obama’s immigration policies, although Senate Democrats have already promised to block such a move.

The House struggle came after the Senate passed its own legislation in the morning, 68 to 31, to fund the department through the fiscal year — even though senators had expected the House to pass its own temporary measure.

The strong Republican vote for the Senate bill also highlighted the deep rift between House and Senate Republicans, who have struggled to agree on a pragmatic path forward to both keep the agency running and express their displeasure with Mr. Obama’s recent immigration actions.

“We should have never fought this battle,” said Senator Mark S. Kirk, Republican of Illinois. “In my view, in the long run, if you are blessed with the majority, you are blessed with the power to govern. If you’re going to govern, you have to act responsibly.”

Just two months into the new Congress, Republicans were sounding a grim note, far removed from their triumphant election victories in November. Senator Lindsey Graham, Republican of South Carolina, said Friday that “2015 is about us.”

“There’s nobody to blame but us now when it comes to the appropriations process,” Mr. Graham said. “If we can run the place more traditional, like a business, so to speak, I think we flourish. If we self-inflict on the budget, and the appropriations process, and we can’t get the government managed well, then I think we’re in trouble.”

In the aftermath of the failed vote, the Republican leadership team met for hours Friday night to come up with a new approach, but their options were limited given the deep rebellion by their more conservative members against supporting anything that does not halt the president’s immigration policies. As the legislation stalled, Mr. Boehner walked wordlessly from the chamber, his head down.

Friday’s crisis represented a perilous situation for the speaker, who since taking control of the House in 2011 has struggled to unite his fractious rank-and-file on spending and policy issues.

Mr. Boehner wanted to avoid a shutdown for which he knows Republicans will be blamed, but he cannot risk getting out too far ahead of his conservative members, who are dug in against the president.

The failed vote was also a rebuke for Representative Steve Scalise of Louisiana, the Republican whip, who ran for the No. 3 position last year on the promise that as a red-state lawmaker he would be able to help persuade recalcitrant conservatives to support leadership proposals.

“Our leadership set the stage for this,” said Representative John Fleming, Republican of Louisiana. “Finally at the last hour we hear, ‘O.K., well give us three weeks and we’ll try to fire the base up and get something done.’ Well what have we been doing for the last eight weeks? We’ve not been doing anything.”

For Mr. Boehner, said Representative Steve Israel of New York, a member of the Democratic leadership, “homeland security is the security of his gavel, and tonight it’s less secure.”

Josh Earnest, the White House press secretary, told reporters earlier that Mr. Obama would sign a short-term bill, if needed. “If the president is faced with a choice of having the Department of Homeland Security shut down or fund that department for a short term, the president is not going to allow the agency to shut down,” he said.

After the Republicans gained control of the Senate and increased their margins in the House in the November elections, both Mr. Boehner and Senator Mitch McConnell of Kentucky, the majority leader, promised to reverse Congress’s pattern of hurtling from crisis to crisis, even over matters like appropriations that were once relatively routine.

But in their first big test, the Republican leaders often seemed to be working from different playbooks, at times verging on hostility, with each saying it was time for the other chamber to act.

The funding stalemate bodes poorly for any larger policy accomplishments this year, leaving lawmakers pessimistic that the 114th Congress will be able to work in a bipartisan fashion on more complicated issues.

The Office of Management and Budget has said that a vote to increase the nation’s debt limit will be necessary by mid- to late summer, and lawmakers were also hoping to take up trade policy, as well as at least a modest overhaul of the nation’s tax code — undertakings that now look increasingly imperiled.

Mr. Obama has already vetoed the Republicans’ main legislative achievement this year — a bill to start construction on the Keystone XL oil pipeline.

“The D.H.S. funding fight is the first test of the new Republican Congress and so far, they’re failing,” said Senator Charles E. Schumer, Democrat of New York.

The fight over the homeland security funding — coupled with a separate revolt by House conservatives — also upended Republican plans to overhaul No Child Left Behind, the 2001 education law that was a signature domestic achievement of President George W. Bush.

Republican leadership had expected to pass a new bill on Friday to reduce the federal government’s role in public education; Mr. Obama has threatened a veto. But the vote was put off after Heritage Action, the conservative advocacy group, waged a campaign against the measure, saying it does not do enough to limit federal authority.