It was the Food and Drug Administration (a federal agency) which had allowed Plan B to be sold over the counter only to 18 year olds. Under the Administrative Procedure Act, federal courts can reverse any decision of a federal agency that is "arbitrary, capricious or not in accordance with law." It appears that the basis for the decision is that the FDA was "arbitrary" because it ignored the recommendation of own panel of scientific advisors.
Just for reference, here is the ruling of the court.
http://www.nyed.uscourts.gov/pub/rulings/cv/2005/05cv366mofinal.pdf