"We cannot be forced back into the closet. We are not backing off from our fight against discrimination," said Gautam Bhan, an activist who had petitioned the court.
After the ruling, dozens of activists outside the court began crying and hugging each other in consolation.
"This is a very sad day for us, we are back to square one in our fight for the democratic rights of the gay community," said Ashok Row Kavi of the activist group Humsafar Trust.
Lawyers and supporters of gays, lesbians and transsexuals vowed to continue pressing for the removal of the law, which they say encourages discrimination, even if it is rarely invoked by prosecutors.
"We feel very let down," said lawyer Anand Grover, who had argued the case on behalf of the advocacy group NAZ Foundation. "But our fight is not over and we will continue to fight for the constitutional right."
He said the foundation would ask for the Supreme Court's decision to be reviewed.
According to international human-rights groups, more than 70 countries around the world have laws criminalizing homosexual conduct, with India by far the most populous. Some other countries, while not explicitly outlawing gay sex, have measures that restrict gay-rights activities, such as Russia's recently enacted law prohibiting "gay propaganda."
Efforts to repeal colonial-era anti-sodomy laws have failed to gain much momentum in Africa and Asia, but there have been notable recent gay-rights gains in Latin America, where same-sex marriage or civil unions have been legalized in Argentina, Uruguay, Brazil and parts of Mexico.
The largest gay-rights group in the United States the Human Rights Campaign described the Indian court ruling as a "disturbing step backward."
"It is incomprehensible that a court of law would take the side of discrimination against LGBT citizens," said the group's chief foundation officer, Jeff Krehely. "Criminalizing LGBT relationships leads to dangerous situations, not just for committed couples, but also for LGBT youth, who today received a deeply harmful message that they are less than equal."
The United States expressed concern about the Indian Supreme Court decision, although it wasn't immediately clear if Washington had directly raised the issue with Indian government officials.
"We oppose all actions that criminalize consensual sex conducts between adults," State Department spokeswoman Jen Psaki told reporters in Washington. "We call on all governments to advance equality for LGBT individuals around the world."
But the court ruling was welcomed by a conservative U.S. legal advocacy group, Alliance Defending Freedom.
"The India Supreme Court has ruled in the interest of the health of its society rather than the interests of activist groups trying to use the court to do their bidding," said the group's chief counsel, Benjamin Bull.
The law in question, dating back to the 1860s when Britain ruled over South Asia, states that "whoever voluntarily has carnal intercourse against the order of nature with any man, woman or animal" can be punished by up to 10 years behind bars.
The 2009 New Delhi High Court ruling, which said the law violated fundamental human rights, infuriated conservatives and religious groups who say homosexuality represents a threat to traditional Indian culture.
In a rare alliance, the groups including the All India Muslim Law Board, Christian groups and Hindu spiritual leaders argued that gay sex is unnatural and that India should maintain the law.
Amod Kanth, head of the Prayas organization for children's welfare, one of India's largest civic groups, cheered Wednesday's ruling and said banning homosexuality is key to ensuring children's normal development and protecting their rights to family.
"Only a man and a woman constitute a family and contribute for the holistic development of a child, which is not possible without a father and a mother," Kanth told the Press Trust of India news agency.
Activists have long argued that the law encourages discrimination and leaves gays, lesbians and bisexuals vulnerable to police harassment or demands for bribes. In a country where arranged marriage is still largely the norm, many gays hide their sexual orientation from friends and relatives.
Acceptance is slowly growing, though, particularly in big cities such as New Delhi and Mumbai. In the last few years, activists have staged large gay pride parades featuring rainbow-colored flags and banners, joyful songs and dancing through the streets.
The government, meanwhile, has begun acknowledging India's transgender or hijra community, the origins of which go back millennia to a time when transsexuals, eunuchs and gays held a special place in society backed by Hindu myths of their power to grant fertility.
In 2009, the government allowed them to register to vote as "others," rather than as men or women. And in 2010, a new "third gender" category was added to the national census.
Law Minister Kapil Sibal said little about Wednesday's verdict beyond agreeing that the "legislature is the final arbiter of what law should be."
If the issue comes up in Parliament, he said, "we will take it up."
AP National Writer David Crary in New York contributed to this report.
Follow Nirmala George on Twitter at twitter.com/NirmalaGeorge1