The court had not conclusively interpreted the Second Amendment since its ratification in 1791. The amendment reads: "A well regulated militia, being necessary to the security of a free state, the right of the people to keep and bear arms, shall not be infringed."
The basic issue for the justices was whether the amendment protects an individual's right to own guns no matter what, or whether that right is somehow tied to service in a state militia.
Writing for the majority, Justice Antonin Scalia said that an individual right to bear arms is supported by "the historical narrative" both before and after the Second Amendment was adopted.
The Constitution does not permit "the absolute prohibition of handguns held and used for self-defense in the home," Scalia said. The court also struck down Washington's requirement that firearms be equipped with trigger locks.
In a dissent he summarized from the bench, Justice John Paul Stevens wrote that the majority "would have us believe that over 200 years ago, the Framers made a choice to limit the tools available to elected officials wishing to regulate civilian uses of weapons."
He said such evidence "is nowhere to be found."
Justice Stephen Breyer wrote a separate dissent in which he said, "In my view, there simply is no untouchable constitutional right guaranteed by the Second Amendment to keep loaded handguns in the house in crime-ridden urban areas."
Joining Scalia were Chief Justice John Roberts and Justices Samuel Alito, Anthony Kennedy and Clarence Thomas. The other dissenters were Justices Ruth Bader Ginsburg and David Souter.
The capital's gun law was among the nation's strictest.
Dick Anthony Heller, 66, an armed security guard, sued the District after it rejected his application to keep a handgun at his home for protection in the same Capitol Hill neighborhood as the court.
The U.S. Court of Appeals for the District of Columbia ruled in Heller's favor and struck down Washington's handgun ban, saying the Constitution guarantees Americans the right to own guns and that a total prohibition on handguns is not compatible with that right.
The issue caused a split within the Bush administration. Vice President Dick Cheney supported the appeals court ruling, but others in the administration feared it could lead to the undoing of other gun regulations, including a federal law restricting sales of machine guns. Other laws keep felons from buying guns and provide for an instant background check.
Scalia said nothing in Thursday's ruling should "cast doubt on long-standing prohibitions on the possession of firearms by felons or the mentally ill, or laws forbidding the carrying of firearms in sensitive places such as schools and government buildings."
The law adopted by Washington's city council in 1976 bars residents from owning handguns unless they had one before the law took effect. Shotguns and rifles may be kept in homes, if they are registered, kept unloaded and either disassembled or equipped with trigger locks.
Opponents of the law have said it prevents residents from defending themselves. The Washington government says no one would be prosecuted for a gun law violation in cases of self-defense.