(In the interest of full disclosure, I did help out in a very minor way in the Comstock appeal, so I'm not a wholly disinterested spectator in this case)
After carefully considering the Government’s arguments, we conclude, for the reasons set forth below, that § 4248 does indeed lie beyond the scope of Congress’s authority. The Constitution does not empower the federal government to confine a person solely because of asserted "sexual dangerousness" when the Government need not allege (let alone prove) that this "dangerousness" violates any federal law. We therefore affirm the judgment of the district court.
At the same time, presumably civil commitment of sexual offenders is aimed at preventing repeat sexual offenses. (Let's set aside whether such civil commitment after the end of a sentence may sometimes deny people liberty in violation of the Due Process Clause; that's an issue unrelated to the federal power question, since it would apply equally to states.) And presumably someone who committed a federal sex crime (e.g., possession or trafficking of child pornography) is pretty likely to commit another crime of much the same variety -- which will likely be a federal crime -- and not just some other random state sex crime. If the Commerce Clause power to regulate commerce authorizes Congress to ban commerce in child pornography, and the Necessary and Proper Clause therefore authorizes Congress to ban even private possession of child pornography, then it's hard to see why the Necessary and Proper Clause wouldn't authorize continued detention of people who have shown a willingness to commit such federal crimes.
One way of thinking about it might be to think about the historically established practice of civil commitment of people found not guilty by reason of insanity. If someone is tried for a federal crime and found insane, he won't be imprisoned for the crime -- since he's not criminally guilty -- but he will be locked up in a mental hospital so long as he is thought to be dangerous. I think that's right, but how does it fit the panel's decision? After all, the person is not guilty, so Congress can't appeal to its power to punish federal criminals (just as the people in this case can't be further criminally punished, since there terms are up). True, we worry that this insane person will commit another crime, but under the panel's reasoning, that might well be a state crime. So must Congress release such people unless it gets a state to agree to take custody of them? Perhaps that's the right answer, since Congress lacks the enumerated power to detain them -- but I'm skeptical that this is so.
Finally, Raich restored the so-called "rational basis" test for judicial review of Commerce Clause cases. In plain English, that means that the government doesn't have to actually prove that Section 4248 regulates "economic activity" or that it is part of a broader regulatory scheme. Rather, the government can win simply by showing that Congress might have had some "rational" reason for believing that one of these two conclusions is correct. And by "rational," the Court means merely that there is some possibility, even if a very remote one, that Congress' putative reasoning might be sound.
Unfortunately, the Comstock decision dismisses Raich in a brief footnote that ignores most of the considerations discussed here. The Fourth Circuit does rely heavily on the Court's two earlier Commerce Clause decisions in United States v. Lopez and United States v. Morrison, but essentially ignores the way in which Raich greatly undercuts those precedents by virtually confining them to their facts. I discuss the impact of Raich on Lopez and Morrison in my article linked above; see also this excellent piece by co-conspirator Jonathan Adler.