The 5-4 decision overturned an earlier court ruling that states have fought for years.