The new bill punishes anyone convicted of having gay sex with jail terms up to life and also makes it a crime to fail to report someone for breaking the new law.
Officials broke into loud applause as President Yoweri Museveni signed the bill into law in a ceremony in front of foreign journalists at his State House in the lakeside town of Entebbe.
He said: "There's now an attempt at social imperialism, to impose social values. We're sorry to see that you (the West) live the way you live but we keep quiet about it." Gay and lesbian organisations in Africa fear the ripple effect from the anti-gay bill could spread beyond Uganda to other parts of a continent where conservative societies tend to view homosexuality as unnatural.
Julian Peppe Onziema, a gay rights campaigner in Uganda, said: "It's a gloomy day not just for the gay community in Uganda but for all Ugandans who care about human rights because this law will affect everybody."
Amnesty International said the law was "wildly discriminatory" and amounted to a grave assault on human rights.
The veteran leader's signature will please many voters opposed to homosexuality ahead of presidential elections in 2016 but risks alienating western aid donors.
The law comes a week after US President Barack Obama said the legislation would be "a step backward for all Ugandans" and warned it would complicate relations.
A senior Obama administration official had said the US would review its relations with Uganda if the law was enacted. Washington is one of Uganda's largest donors, sending more than £240 million a year in recent years.
Uganda is a key western ally in the fight against Islamic extremism in Somalia where Ugandan troops have formed the backbone of a peacekeeping force.