WASHINGTON — In a historic vote for gay rights, the Senate agreed on Saturday to do away with the military's 17-year ban on openly gay troops and sent President Barack Obama legislation to overturn the Clinton-era policy known as "don't ask, don't tell."

The Senate also Saturday defeated by five votes the Deam Act that would have given hundreds of thousands of young illegal immigrants a path to legal status if they enrolled in college or joined the military.

Obama was expected to sign the "don't ask, don't tell" repeal into law next week, although changes to military policy probably wouldn't take effect for at least several months. Under the bill, the president and his top military advisers must first certify that lifting the ban won't hurt troops' ability to fight. After that, the military would undergo a 60-day wait period.

Repeal would mean that, for the first time in American history, gays would be openly accepted by the armed forces and could acknowledge their sexual orientation without fear of being kicked out.

More than 13,500 service members have been dismissed under the 1993 law.

"It is time to close this chapter in our history," Obama said in a statement. "It is time to recognize that sacrifice, valor and integrity are no more defined by sexual orientation than they are by race or gender, religion or creed."

The Senate voted 65-31 to pass the bill, with eight Republicans siding with 55 Democrats and two independents in favor of repeal. The House had passed an identical version of the bill, 250-175, last week.

Supporters hailed the Senate vote as a major step forward for gay rights. Many activists hope that integrating openly gay troops within the military will lead to greater acceptance in the civilian world, as it did for blacks after President Harry Truman's 1948 executive order on equal treatment regardless of race in the military.

"The military remains the great equalizer," said Sen. John Kerry, D-Mass. "Just like we did after President Truman desegregated the military, we'll someday look back and wonder what took Washington so long to fix it."

Sen. John McCain, Obama's GOP rival in 2008, led the opposition. Speaking on the Senate floor minutes before a crucial test vote, the Arizona Republican acknowledged he couldn't stop the bill. He blamed elite liberals with no military experience for pushing their social agenda on troops during wartime.

"They will do what is asked of them," McCain said of service members. "But don't think there won't be a great cost."

How the military will implement a change in policy, and how long that will take remains unclear. Senior Pentagon officials have said the new policy could be rolled out incrementally, service by service or unit by unit.

In a statement issued immediately after the vote, Defense Secretary Robert Gates said he will begin the certification process immediately. But any change in policy won't come until after careful consultation with military service chiefs and combatant commanders, he said.

"Successful implementation will depend upon strong leadership, a clear message and proactive education throughout the force," he said.Adm. Mike Mullen, chairman of the Joint Chiefs of Staff, said he welcomes the change.

"No longer will able men and women who want to serve and sacrifice for their country have to sacrifice their integrity to do so," he said. "We will be a better military as a result."

Sen. Carl Levin, a chief proponent of repeal, said he has received a commitment from the administration that it won't drag its heels.

"We hope it will be sooner, rather than later," he said.

The fate of "don't ask, don't tell" had been far from certain earlier this year when Obama called for its repeal in his State of the Union address. Despite strong backing from liberals in Congress, Republicans and conservative Democrats remained skeptical that lifting the ban could be done quickly without hurting combat operations in Iraq and Afghanistan.

In February, Mullen provided the momentum Obama needed by telling a packed Senate hearing room that he felt the law was unjust. As chairman of the Joint Chiefs of Staff, Mullen became the first senior active-duty officer in the military to suggest that gays could serve openly without affecting military effectiveness.

"No matter how I look at the issue," Mullen said, "I cannot escape being troubled by the fact that we have in place a policy which forces young men and women to lie about who they are in order to defend their fellow citizens."

With Mullen's backing, Gates ordered a yearlong study on the impact, including a survey of troops and their families.

The study, released Nov. 30, found that two-thirds of service members didn't think changing the law would have much of an effect. But of those who did predict negative consequences, most were assigned to combat arms units. The statistic became ammunition for opponents of repeal, including the service chiefs of the Army and Marine Corps.

"I don't want to lose any Marines to the distraction," Gen. James Amos, head of the Marine Corps, told reporters. "I don't want to have any Marines that I'm visiting at Bethesda (Naval Medical Center) with no legs be the result of any type of distraction."

Mullen and Gates counter that the fear of disruption is overblown and could be addressed through training. They note the Pentagon's finding that 92 percent of troops who believe they have served with a gay person saw no effect on their units' morale or effectiveness.

But even with backing from Gates and Mullen, the bill appeared all but dead this month when Senate Republicans united against it on procedural grounds. In last-minute wrangling, Senate Majority Leader Harry Reid was able to revive the bill during the rare Saturday session with just days to go before the lame-duck session was to end.

The Republicans who voted for repeal said the Pentagon study on gays and assurances from senior military leaders played a crucial role.

"The repeal of 'don't ask, don't tell' will be implemented in a common sense way," said Ohio Republican Sen. George Voinovich. "Our military leaders have assured Congress that our troops will engage in training and address relevant issues before instituting this policy change."

Advocacy groups were jubilant following the Senate's initial test vote that passed 63-33 and set up final passage. The Servicemembers Legal Defense Network called the issue the "defining civil rights initiative of this decade." Supporters of repeal filled the visitor seats overlooking the Senate floor, ready to protest had the bill failed.

"This has been a long-fought battle, but this failed and discriminatory law will now be history," said Joe Solmonese, president of the Human Rights Campaign.

At least 25 countries allow gays to serve openly in the armed forces, among them Britain, Canada and Israel, according to the Palm Center, a research institute at the University of California, Santa Barbara.

As for the DREAM Act, its sponsors fell five votes short of the 60 they needed to break through largely GOP opposition and win its enactment before Republicans take over the House and narrow Democrats' majority in the Senate next month.

Obama called that vote "incredibly disappointing."

"A minority of senators prevented the Senate from doing what most Americans understand is best for the country," Obama said. "There was simply no reason not to pass this important legislation."

Dozens of immigrants wearing graduation mortarboards watched from the Senate's visitors gallery, disappointment on their faces, as the 55-41 vote was announced.

"This is a dark day in America," said Jorge-Mario Cabrera, a spokesman for the Coalition for Humane Immigrant Rights in Los Angeles. "The Senate has ... thrown under the bus the lives and hard work of thousands and thousands of students who love this country like their own home, and, in fact, they have no other home."

Hispanic activists and immigrant advocates had looked to the bill as a down payment on what they had hoped would be broader action by Obama and the Democratic-controlled Congress to give the nation's 10 million to 12 million illegal immigrants a chance at legal status.

It targeted the most sympathetic of the millions of illegal immigrants — those brought to the United States as children, who in many cases consider themselves American, speak English and have no ties to or family living in their native countries.

"They stand in the classrooms and pledge allegiance to our flag," said Sen. Dick Durbin, D-Ill., the bill's chief sponsor. "This is the only country they have ever known. All they're asking for is a chance to serve this nation."

Critics called the bill a backdoor grant of amnesty that would encourage more foreigners to sneak into the United States in hopes of being legalized eventually.

"Treating the symptoms of the problem might make us feel better ... but it can allow the underlying problem to metastasize," said Sen. Jon Kyl, R-Ariz. "Unfortunately, that's what's happening at our border."

The legislation would have provided a route to legal status for an estimated 1 million to 2 million illegal immigrants who were brought to the U.S. before age 16, have been here for five years, graduated from high school or gained an equivalency degree and who joined the military or attend college. Democrats' determination to vote on the bill before year's end reflected the party's efforts to satisfy Hispanic groups whose backing has been critical in recent elections and will be again in 2012. They said they'll try again in the next Congress, despite the increased GOP presence.

"The echo of this vote will be loud and long," said Rep. Luis V. Gutierrez, D-Ill., a key House sponsor of the bill. "We are at the tipping point that will define the political alignment of the Republican and Democratic parties with Latino voters for a generation."

"This country has a history of opening its arms," said Sen. Barbara Boxer, D-Calif. "Today, it's arms were closed, but we're going to get there."

Three Republicans — Robert Bennett of Utah, Lisa Murkowski of Alaska and Richard Lugar of Indiana — joined 50 Democrats and the Senate's two independents in voting for the bill.

Five Democrats — Max Baucus and Jon Tester of Montana, Kay Hagan of North Carolina, Ben Nelson of Nebraska and Mark Pryor of Arkansas — joined 36 Republicans in blocking it. Not voting were Republican Sens. Jim Bunning of Kentucky, Orrin Hatch of Utah and Judd Gregg of New Hampshire, and Democratic Sen. Joe Manchin of West Virginia.

Utahns' votes

Sen. Bob Bennett

No on "don't ask, don't tell."

Yes on DREAM Act

Sen. Orrin Hatch

Did not vote on either bill.