KAMPALA, Uganda—Uganda’s constitutional court on Wednesday upheld large parts of a sweeping anti-LGBTQ law that has prompted the U.S. to expel the East African country from a preferential trade deal, and the World Bank and others to suspend some development funding.

Justice Richard Buteera said the panel of five judges had decided to strike down several sections of the legislation, but said significant parts of the law, known as the Anti-Homosexuality Act , were in line with Uganda’s constitution.

The court upheld a provision that imposes prison sentences of up to 20 years for what the law calls promoting homosexuality and which effectively criminalizes teaching students that it is natural for some people to be attracted to someone of the same sex. It also affirmed colonial-era legislation that already allows a life sentence for same-sex intercourse.

But the judges nullified other sections of the law, including making it a crime to let “premises for homosexual purposes” or failure to report “acts of homosexuality” to the police.

“We find that such provisions violate the right to privacy,” Buteera said, while reading the ruling in a crowded courtroom in the Ugandan capital. “We also find that some of the provisions in the act violate the right to health as enshrined in the Ugandan laws.”

The court also struck down a provision that allowed the death penalty for the unintentional transmission of HIV and other terminal illnesses during same-sex relations. It upheld the death penalty for other crimes that the law refers to as “aggravated homosexuality,” including same-sex relations with a minor or a disabled person.

Frank Mugisha, a Ugandan gay-rights activist and one of the petitioners who had challenged the law at the constitutional court, said he would appeal the ruling at Uganda’s Supreme Court. “Our community members are now on high alert with the draconian law being declared constitutional,” Mugisha said. “The ground has been set for unprecedented levels of violence.”

Uganda’s attorney general didn’t immediately respond to request for comment on whether the government plans to appeal.

Since President Yoweri Museveni signed the law in May, many openly gay Ugandans have gone underground, while hundreds have fled the country. At least 58 people have been arrested and five formally charged under the legislation in Ugandan courts.

In August, police officers raided a massage parlor in Eastern Uganda, where they arrested four people suspected of having same-sex intercourse. A month later, a 20-year old man was arrested and charged in court for having sex with a 41-year old man.

Activists have also documented 21 forced anal examinations ordered by police officers investigating cases of alleged same-sex relations. Some have reported violent assaults they attributed to a new climate of impunity when it comes to LGBTQ people.

There is broad support for the legislation in Ugandan society, which is predominantly Christian and socially conservative. Only two lawmakers voted against the legislation in parliament last year.

But Uganda has been under intense international pressure including from the U.S., the European Union and the United Nations, which have said that the law violates human rights and undermines efforts to combat HIV and AIDS in the country.

The Biden administration in January expelled Uganda from the African Growth and Opportunity Act, making Ugandan goods ineligible for preferential access to the U.S. market. Washington has also imposed visa restrictions on Ugandan officials, cut back defense funding for the government in Kampala and redirected financing for HIV and AIDS relief to nongovernmental organizations.

The World Bank, which runs a $5.2 billion portfolio of projects in Uganda, suspended lending on new projects, saying the law fundamentally contradicted its values. The Netherlands has frozen some $27 million of security assistance to Uganda.

The U.S. sanctions in particular put a major strain on relations with one of Washington’s key African security allies, which for years has supported the fight against terrorism in Somalia and elsewhere. The U.S. sends around $1 billion to Uganda each year to fund projects in the military, business and health sectors.

In public, Museveni has sought to play down the threats posed by the international aid cuts, accusing the World Bank and other donors of trying to undermine Uganda’s sovereignty. Behind the scenes, however, Museveni, who has ruled Uganda for close to four decades, lobbied Western donors to delay sanctions until the court ruled on the law, according to people familiar with the matter.

In December, he dispatched his trade adviser and son-in-law, Odrek Rwabwogo, to Washington for talks with the administration’s assistant trade representative for Africa, Constance Hamilton, and its special envoy for LGBTQ rights, Jessica Stern.

“What we are saying to the U.S. is that can we speak about this law without these deadlines?” Rwabwogo said in televised remarks after the visit. “I am a moderate person, a lot can change about this law if you give us time.”

In 2014, a Ugandan court struck down an earlier antigay law, determining that it had been passed without the necessary number of lawmakers present.

Oryem Nyeko, a Ugandan researcher for Human Rights Watch, said Wednesday’s ruling creates new dangers for LGBTQ Ugandans.

“This was a real missed opportunity for the court to uphold the fundamental human rights of sexual minorities,” Nyeko said. “The ruling is the entrenching violence and discrimination that exists against LGBT people in Uganda.”

Write to Nicholas Bariyo at nicholas.bariyo@wsj.com