More than 14 million people a year use public library computers, including many children, and the court said patrons of all ages were being exposed to unseemly sex sites on the Web.
Justices ruled that the government can withhold money from libraries that won't install blocking devices, even though the technology shuts off more than pornography.
"To the extent that libraries wish to offer unfiltered access, they are free to do so without federal assistance," the main ruling said.
The 6-3 ruling, although fractured, was the federal government's most significant legal victory in a seven-year effort to shield children from Internet smut.
Four justices said the law didn't violate First Amendment free speech, and two others said it was allowable as long as libraries disable the filters for adult patrons who ask. The law doesn't specifically require the disabling.
"This is electronic book burning. The Supreme Court has ruled the secret censors may prevent you from reading what you want," said Seth Finkelstein, a Cambridge, Mass., computer programmer who is a leading expert on Internet filters.
Judith Krug, with the American Library Association, predicted that many libraries would consider rejecting federal money rather than installing filters. "A substantial number of libraries will say it's not worth it," she said. "The fact that the librarian can flick a switch isn't going to change the stigma that's attached to it."
Chief Justice William H. Rehnquist said, "The Constitution doesn't guarantee the right to acquire information at a public library without any risk of embarrassment."
At a public library Monday, great-grandmother Susannah Clarke said she was glad the court upheld the Children's Internet Protection Act.
"Sex is something that's like a gun: dangerous if you don't know how to use it. I'm all for them putting regulations in a public place," said Clarke, who visits her library branch in Washington, D.C., twice a week.
Rita Thompson-Joyner, a 30-year librarian in Washington, said when it comes to keeping youngsters away from porn, "We don't believe it's the library that has that responsibility. We believe it rests with the parents and only the parents."
Congress has passed three anti-pornography laws since 1996. The Supreme Court struck down the first one, and the second was blocked by justices from taking effect. The first two laws were criminal statutes involving Web-site operators.
Rep. Ernest Istook, R-Okla., a main drafter of the law, said the ruling "will mean libraries can continue to fulfill their mission because parents won't need to be reluctant about dropping off their kids for an afternoon at the library."
Adult library users will be inconvenienced but will still be able to use filterless computers as long as they ask, said Chris Hansen, senior attorney for the American Civil Liberties Union. He said libraries in poor communities will be more likely to install filters because they can't afford to lose the money.
A three-judge federal panel in Pennsylvania ruled last year that the law was unconstitutional because it caused libraries to violate the First Amendment. The Supreme Court disagreed. Rehnquist's opinion was joined by Justices Sandra Day O'Connor, Antonin Scalia and Clarence Thomas.
Justices Anthony M. Kennedy and Stephen Breyer, in separate opinions, said the government's interest in protecting young library users from inappropriate material outweighs the burden on library users having to ask staff to disconnect filters.
"A statutory blunderbuss that mandates this vast amount of overblocking abridges the freedom of speech protected by the First Amendment," Stevens wrote.
Souter compared the filters to a library "buying an encyclopedia and then cutting out pages with anything thought to be unsuitable for all adults."
Opponents of the law said the court's ruling left open other possible constitutional challenges, including lawsuits over the way filters affect library patrons.
The case is United States v. American Library Association, 02-361.