WASHINGTON — Online shopping will soon become more expensive after the U.S. Supreme court ruled Thursday that states can require internet retailers to collect sales taxes.

The 5-4 decision broke with 50 years' worth of legal rulings that barred states from imposing sales taxes on most purchases their residents make from out-of-state retailers.

The decision was a victory for South Dakota, which had asked the court to uphold its recently passed law imposing an internet sales tax.

"Our state is losing millions for education, health care and infrastructure, and our citizens are harmed by an uneven playing field," said Marty Jackley, South Dakota's attorney general.

President Donald Trump gave the issue renewed visibility when he tweeted in early April that Amazon pays "little or no taxes to state & local governments." That wasn't correct: The company has collected taxes since April 1, 2017, on sales to customers in the 45 states that collect them. That leaves out only Alaska, Delaware, Montana, New Hampshire and Oregon.

On Thursday, Trump called the court's ruling a "great victory for consumers" — even though they will be paying more.

Big Supreme Court win on internet sales tax - about time! Big victory for fairness and for our country. Great victory for consumers and retailers. — Donald J. Trump (@realDonaldTrump) June 21, 2018

South Dakota said the states could pick up a combined $34 billion a year if the court allowed them to tax internet sales. But the General Accounting Office estimated that the number would be at most about $13 billion.

"The Supreme Court applied bacon grease to the slippery slope of states taxing and regulating outside their borders," said Andrew Moylan, executive vice president of the conservative National Taxpayers Union Foundation and head of its Interstate Commerce Initiative.

"By validating South Dakota's law, the Court has granted states the power to tax any business anywhere in the country simply for daring to use the internet to access a nationwide market," he added. "Congress must now act to contain the fallout of this case."

In 1967, the Supreme Court ruled that states could not force mail-order catalog companies to collect sales taxes unless a buyer lived in a state where the company had a physical presence — a retail store, a headquarters or a distribution center, for example. The court reasoned then that the volume of mail order business was minor compared to in-store sales and that catalog companies would face too big a burden in having to figure out the correct sales tax, given widely different rates around the country.

Justice Anthony Kennedy, who wrote the majority opinion, said the physical presence rule put brick-and-mortar businesses at a disadvantage, because they had to charge sales taxes but internet retailers did not. That rule "prevented market participants from competing on an even playing field," Kennedy wrote.

Some big retailers that sell online already charge sales taxes. WalMart, for example, has stores in all 50 states, so it is required to do so under the physical presence rule.

South Dakota concluded in 2016 that the explosion in online sales changed the market drastically. So it passed a law requiring all but the smallest retailers, including internet companies, to collect taxes on the sales they make in the state, even if they had no physical presence there.

Internet companies opposed to the South Dakota law appealed. They said local tax laws vary widely across the nation and calculating sales taxes for 10,000 local taxing jurisdictions remains a daunting task.

Retailer eBay warned that taxing internet sales would place "crushing burdens on small online businesses, causing many to curtail operations and damaging the national economy."

Thursday's ruling did not require all Internet companies to collect sales taxes. While larger companies certainly are now obligated to do so, the decision suggested that small retailers, who sell on eBay, for example, will not face the requirement.

The decision praised South Dakota for exempting such low-volume dealers, and states eager to charge the tax would likely escape legal challenges if they copied the South Dakota model.

Chief Justice John Roberts, in the dissenting opinion, said the internet economy has thrived under the sales tax exemptions. "Any alternation to those rules with the potential to disrupt the development of such a critical segment of the economy should be undertaken by Congress," he wrote.