WASHINGTON — In a broad endorsement of federal power, the Supreme Court on Monday ruled that Congress has the authority under the Constitution to allow the continued civil commitment of sex offenders after they have completed their criminal sentences.

The 7-to-2 decision touched off a heated debate among the justices on a question that has lately engaged the Tea Party movement and opponents of the new health care law: What limits does the Constitution impose on Congress’s power to legislate on matters not specifically delegated to it in Article I?

The federal law at issue in the case allows the government to continue to detain prisoners who had engaged in sexually violent conduct, suffered from mental illness and would have difficulty controlling themselves. If the government is able to prove all of this to a judge by “clear and convincing” evidence — a heightened standard, but short of “beyond a reasonable doubt” — it may hold such prisoners until they are no longer dangerous or a state assumes responsibility for them.

The challenge to the civil commitment law was brought by five prisoners. The case of Graydon Comstock was typical. In November 2006, six days before Mr. Comstock was to have completed a 37-month sentence for receiving child pornography, Attorney General Alberto R. Gonzales certified that Mr. Comstock was a sexually dangerous person.