But in a 4-to-3 ruling, the Connecticut justices upheld another element of the families’ case, saying the lawsuit could continue to trial under the state consumer protection law. “Connecticut law does not permit advertisements that promote or encourage violent, criminal behavior,” the decision said.

The lawsuit, filed in 2014 by the families of nine victims and a teacher who was shot and survived, accuses Remington of using militaristic and hypermasculine advertising for its Bushmaster AR-15-style rifle in order to attract disturbed young men like the Sandy Hook killer. On its website, Bushmaster advertised the rifle with the “man card” slogan and the tagline, “If it’s good enough for the military, it’s good enough for you.”

The 2005 federal law, called the Protection of Lawful Commerce in Arms Act, was the culmination of an extended effort by the National Rifle Association to create a blanket protection for gun companies from most liability lawsuits. When the bill passed, the organization noted its seven-year campaign for the law, including “a comprehensive legislative and election strategy,” according to its chief lobbyist.

At the time, the firearms industry and President George W. Bush, who signed the law, said it was necessary to protect gun makers from “frivolous lawsuits” that could bankrupt the companies. Wayne LaPierre, the chief executive of the National Rifle Association, called the law “historic,” saying its passage by Congress was the industry’s most significant legislative victory in 20 years.

Critics, including scores of law professors who signed a letter to Congress protesting the bill in 2005, have said the law extends protection to the firearms industry that few other industries enjoy.

Several previous legal challenges to the shield law have failed, but the Sandy Hook case is the first to take on the industry in this way, said Adam Winkler, a constitutional law expert at the U.C.L.A. School of Law who has written about gun rights.

The nearly five years that the Sandy Hook case has spent winding through the court system “shows you how strong this immunity really is,” Mr. Winkler said.