WASHINGTON — President Obama signed an executive order Monday that bars companies that do work for the federal government from discriminating against employees on the basis of sexual orientation or gender identity.

Religious groups had lobbied Obama to exclude faith-based institutions – such as universities, hospitals and nursing homes – from the order, but he declined to add an exemption. Instead, he maintained existing language that allows religious groups to favor people of their religion for religious roles, such as members of the clergy.

Gay rights groups hailed the president’s action. A key Democratic constituency, they had been pushing for the order for years without any additional religious exemption. Loud cheers and a standing ovation greeted Obama at a packed ceremony Monday at the White House.

“Thanks to your passionate advocacy and the irrefutable rightness of your cause, our government – government of the people, by the people and for the people – will become just a little bit fairer,” the president said in the East Room.

He signed the bill into law flanked by two blue screens that read “Opportunity for all.” Virginia Gov. Terry McAuliffe, a Democrat whose first act as governor this year was to sign a similar order, joined Obama onstage.

“With this action, President Obama has cemented his legacy as a transformative leader,” said Chad Griffin, the president of the Human Rights Campaign, an LGBT advocacy group. “Consistently, this administration has taken unprecedented and historic executive actions to advance LGBT equality in this country and around the world.”

More than 150 conservative religious groups and leaders sent a letter to Obama last month that said “any executive order that does not fully protect religious freedom will face widespread opposition and will further fragment our nation.” Another group of leaders, many with close ties to the president, wrote him July 1 to ask that “an extension of protection for one group not come at the expense of faith communities whose religious identity and beliefs motivate them to serve those in need.”

Stephen Schneck, the director of the Institute for Policy Research & Catholic Studies at The Catholic University of America, who signed the July 1 letter, said the order didn’t offer “the nuanced exemption for religious positions” the group wanted but that he was pleased Obama had retained the 2002 exemption approved by former President George W. Bush.

“I’m sure that some religious groups and perhaps even some LGBT groups will be disappointed,” he said. “It leaves details to litigation. Nevertheless, with the help of the courts, I believe that the administration has left open a path that religious groups can work with.”

The Bush-era language struck a satisfactory balance for many gay-rights and religious organizations, but not everyone. The Family Research Council, a conservative policy group, said the order might force people who depended on government work out of business.

“At its worst, it will bully into silence every contractor and subcontractor who disagrees with homosexual behavior,” it said.

The Rev. Dr. C. Welton Gaddy, the president of Interfaith Alliance, praised Obama’s action but said he’d hoped it would undo Bush’s language.

Recently, the Supreme Court ruled that family-owned corporations don’t have to offer contraceptives on their employee health insurance plans as required under federal law if that conflicts with the owners’ religious beliefs.

Some gay rights groups withdrew support for legislation that would ban discrimination in the workplace based on sexual orientation because they fear the inclusion of broad religious exemptions.

The Employment Non-Discrimination Act passed the Democratic-controlled U.S. Senate, but has stalled in the Republican-led House of Representatives.

The president had long resisted issuing an order because he said he wanted Congress to pass legislation. But after years of inaction on Capitol Hill and intense lobbying by gay rights groups, he decided to act.

Obama amended a pair of existing executive orders – one signed by former President Lyndon Johnson for federal contractors and one signed by former President Richard Nixon for federal employees – to add sexual orientation and gender identity to a list of protections. They will likely take effect early next year.

It’s legal to fire or to refuse employment to someone based on sexual orientation in 29 states, while 32 states lack explicit laws banning discrimination based on gender identity, according to the Human Rights Campaign.

The executive order will apply only to companies that contract with the federal government, but the Human Rights Campaign said those businesses employed more than 20 percent of the American workforce – 28 million people – and collected around $500 billion in federal contracts every year.

Religious organizations provide services such as overseas development programs and re-entry programs for inmates.