“It is not only the right thing to do for talented young people who seek to serve a country they know as their own, it is the right thing for the United States of America,” Mr. Obama said. “Our nation is enriched by their talents and would benefit from the success of their efforts.”

“The Dream Act is important to our economic competitiveness, military readiness, and law enforcement efforts,” he said, adding, “It is disappointing that common sense did not prevail today but my administration will not give up.”

In a floor speech, Senator Richard J. Durbin, Democrat of Illinois, a main champion of the Dream act, urged a yes vote. “I want to make it clear to my colleagues, you won’t get many chances in the United States Senate, in the course of your career, to face clear votes on the issue of justice,” he said.

“Thousands of children in America who live in the shadows and dream of greatness,” he said. “They are children who have been raised in this country. They stand in the classrooms and pledge allegiance to our flag. They sing our ‘Star-Spangled Banner’ as our national anthem. They believe in their heart of hearts this is home. This is the only country they have ever known.”

At a news conference after the vote, Senator Michael Bennet, Democrat of Colorado and a former superintendent of the Denver school system, said he was thinking about all the students he knew there as he cast his vote in favor of the bill.

“Please don’t give up,” Mr. Bennet said. “Don’t be disappointed because we couldn’t get our act together.”

But opponents of the measure said it was too broad and would grant amnesty to illegal immigrants.

“As part of this legislative session there has been no serious movement to do anything that would improve the grievous situation of illegality at our border,” said Senator Jeff Sessions of Alabama, who led the opposition to the bill as the senior Republican on the Judiciary Committee. “Leaders in Washington have not only tolerated lawlessness but, in fact, our policies have encouraged it.”