Mr Obama said the law meant that tens of thousands of Americans would no longer be asked to live a lie.
He had campaigned to change the 1993 “don’t ask, don’t tell” law, overturned by Congress last week.
More than 13,000 service members have been dismissed under the policy, enacted in 1993 as a compromise.
Opponents argue that the change will damage troop morale at a time of war.
But earlier this month, a Pentagon report said that allowing openly gay troops would have little impact on the cohesion of US forces fighting in Iraq and Afghanistan.
The outgoing Senate and House of Representatives approved the new law last week, with moderate Republicans joining the Democratic majority.
So many activists were expected at the signing ceremony that the White House booked a larger room for the event.
“This is a very good day,” Mr Obama said as he introduced the law, surrounded by senior military officials and members of Congress.
Continue reading the main story
No longer will our country be denied the service of thousands of patriotic Americans who were forced to leave the military, regardless of their skills, no matter their bravery or their zeal, no matter their years of exemplary performance because they happen to be gay”
“This morning I’m proud to sign a law that will bring an end to ‘don’t ask, don’t tell’.
“No longer will tens of thousands of Americans be asked to live a lie in order to serve the country that they love.”
The president said the new law would be good for the armed forces.
“The law that I’m about to sign will strengthen national security and uphold ideals that fighting men and women risk their lives to defend,” he said.
“No longer will our country be denied the service of thousands of patriotic Americans who were forced to leave the military, regardless of their skills, no matter their bravery or their zeal, no matter their years of exemplary performance because they happen to be gay.”
But correspondents say that the planned celebration parties by gay rights campaigners and members of the military may be premature.
Guidelines need to be finalised on practical questions ranging from how to educate troops to how sexual preference should be handled when army staff are organising sleeping arrangements in military barracks.
Those guidelines need to be signed off by President Obama, Defence Secretary Robert Gates and Admiral Mike Mullen, chairman of the Joint Chiefs of Staff.
And even once that is complete, the new law will not officially take effect for another 60 days. Until then, the current ban remains in place.
However, Mr Obama said there would be no foot-dragging to implement the law.