The House of Lords spent two days debating the Marriage (Same Sex Couples) Bill, with many members voicing their concerns.
But it rejected an amendment aimed at wrecking the bill by 242 votes, moving it a step closer to becoming law.
The BBC's Norman Smith said plans were on course for the first same-sex weddings to take place next summer.
The bill would allow couples, who can currently form civil partnerships, to marry.
If it passes into law, religious organisations would have to "opt in" if they wished to offer gay weddings, except the Church of England and Church in Wales, which would be banned in law from doing so.
Peers were allowed a free vote on the amendment, tabled by crossbench peer Lord Dear, which would effectively have wrecked the government's plans. It was defeated by 390 votes to 148.
Shortly afterwards, the bill - also backed by Labour leader Ed Miliband - was given a second reading without a vote taking place and will now go forward to more detailed scrutiny by peers.
The result was greeted with cheers from supporters of same-sex marriage gathered outside Parliament.
During the debate, Lord Dear insisted the change would "completely alter the concept of marriage as we know it".
The bill was "ill thought through", had no democratic legitimacy and was "fatally flawed", he said.
But equalities minister Baroness Stowell of Beeston called the legislation a "force for good" which would strengthen marriage.
She said it protected both religious freedom and freedom of speech.
Baroness Royall, Labour's leader in the Lords, said marriage had a "special status in our society":  "I firmly believe that our society will be strengthened when more couples are able to choose to make a lifetime commitment to each other and when all members of our communities are able to celebrate their identity and relationships within the institution of marriage."
After the vote, Culture Secretary Maria Miller posted on Twitter: "Great result in @UKHouseofLords tonight, overwhelming support from Peers from all sides."
Ben Summerskill, chief executive of the gay rights group Stonewall, said he was delighted, adding: "Britain's 3.7 million gay people don't deserve to be second class citizens in their own country.
"A tough fight lies ahead and we'll continue to work tirelessly every single day to get equal marriage through the Lords."
Peter Tatchell, who co-ordinates the Equal Love campaign, said: "This is a victory for love, marriage and equality.
"We are another step closer to our goal of equal marriage. It signals that the House of Lords accepts the principle that we should all be equal before the law."
But Colin Hart, campaign director for the Coalition for Marriage, said that 148 peers had "chosen to register their profound opposition to the gay marriage bill".
He added: "The government may have won the vote today but what was clear from the debate was the huge opposition to almost every part of the bill.
"We will continue to campaign to save traditional marriage and today's vote and the concerns expressed by many peers mean we will be able to introduce safeguards that will protect teachers, registrars, chaplains and anyone who works in the public sector.
"If the government refuse to accept these changes, they risk losing the legislation at third reading."
The plans, which the government wants to come into force in July next year, passed through the Commons last month with a 205 majority.
But many religious bodies, including the Church of England, and many Conservative activists have raised concerns. The Archbishop of Canterbury, Justin Welby, was among those who spoke against the proposal in the Lords.
France recently held its first same-sex marriage while the Scottish government has confirmed it will introduce its own bill shortly.
In total Lord Dear's amendment was supported by 66 Conservative peers, 16 Labour peers, two Liberal Democrats, 46 crossbenchers, nine bishops and nine others.
Supporting the Bill were 80 Conservatives, 160 Labour peers, 73 Lib Dems, 68 crossbenchers and nine others.
Peers have voted by more than two to one to back government plans for same-sex marriages in England and Wales.