However, the court added that religious officials cannot be forced to perform unions against their beliefs, and the legislation to allow gay marriage must still pass with a majority of the House of Commons.
Prime Minister Paul Martin said after the court's ruling that since judges in six Canadian provinces and one of its territories are already allowing gay marriages, it should be approved throughout the country. He said his government would introduce a bill shortly after the Christmas holidays.
He noted that members of Parliament would be free to vote their conscience, but his Cabinet ministers would have to support the government's bill.
"For many Canadians and many parliamentarians, this is a difficult issue involving personal and religious convictions and it represents a very significant change to a long-standing institution," Martin said.
Canada would join Belgium and the Netherlands in allowing gay marriage if the government makes it legal nationwide.
The ruling did not say whether allowing same-sex marriage is required by the constitution.
However, it noted the federal government, by not fighting a number of lower court rulings, had accepted the position that barring gays from marriage is discriminatory.
The court's decision brings to the final stages a long, bitter fight over whether gays and lesbians should be allowed to marry in Canada. Public opinion is evenly divided on the matter, and advocates for both sides are preparing for the final phase of the battle.
"This is a victory for Canadian values," said Alexander Munster of Canadians for Equal Marriage.
In the United States, gay marriage is opposed by a majority of Americans, according to an Associated Press-Ipsos poll taken in November, shortly after constitutional amendments in 11 states to ban same-sex marriage were approved.
Malcolm Lazin, executive director of the gay-rights group Equality Forum in Philadelphia, said the Supreme Court's decision would help those seeking same-sex marriages in the United States.
"I think the important thing is that Canada's really leading the way, they really have become a role model for all of North America," he said in an interview with CBC television news.
To pass in the House of Commons, the legislation needs the approval of about 44 of the 95 Liberal backbench members of Parliament to obtain a 155-vote majority in the 308-seat House.
One top Liberal predicted the legislation should pass easily after its introduction, likely early next year. It already has the support of the 38-member Liberal cabinet and virtually all the 54 Bloc Quebecois and 19 New Democrat MPs.
However, some Liberal members of Parliament are opposed.
"I do personally have a problem with redefining marriage and I'm sure some of my colleagues do as well," said Roy Cullen of the Liberal Party.
Gordon Young, pastor of the First Assembly of God Church in St. John's, Newfoundland, was highly disappointed by the ruling.
"It's a sad day for our country," Young told CBC television news. "God is in the DNA of this nation. We believe that changing the definition of marriage is changing the divine institution that God put in place for the order of our society."
The ruling said the legal definition of marriage should change with public opinion over time.
"Several centuries ago, it would have been understood that marriage be available only to opposite-sex couples," the court said in its advisory opinion. "The recognition of same-sex marriage in several Canadian jurisdictions as well as two European countries belies the assertion that the same is true today."
The opinion is not legally binding and it's now up to the federal government to make gay marriage protected by law nationwide.
The high court opinion comes 18 months after former Prime Minister Jean Chretien abandoned his government's fight against same-sex marriage by refusing to appeal provincial court rulings in Ontario, British Columbia and Quebec, which declared traditional marriage laws were unconstitutional.
His government then drafted legislation that would allow gay and lesbian weddings in city halls, courthouses and in religious institutions that choose to perform them. To ensure the bill would be passed, the Liberal government asked the Supreme Court three questions:
Prime Minister Paul Martin expanded the reference after he was sworn in a year ago, adding a fourth question: Is the traditional definition of marriage — between one man and one woman — also constitutional? This was aimed at clarifying once and for all whether the century-old definition of marriage is flawed. The court did not answer it.
The federal Conservatives and several Liberal MPs are expected to fight to preserve marriage for heterosexuals.