The ECJ said that though the EU is fundamentally a trading bloc founded on the principle of free trade between members, countries could restrict the gambling trade if they did it in a consistent way and to protect the interests of their citizens.
The Netherlands bans gambling except that conducted by one licensed operator in each of two markets: sports and lottery betting, and a horse racing totalisation service.
Betfair owner Sporting Exchange wanted to offer its betting market in the Netherlands but was told by the Dutch government that it could not. It lost subsequent cases there and a Dutch court eventually asked the ECJ if the Dutch government was within its rights to bar gambling companies from operating there.
Betfair argued that it should be allowed to operate in the Netherlands and that EU free trade laws mean that it should not have to be regulated in the Netherlands when it had already been regulated in the UK.
The ECJ acknowledged the free trade laws, saying that while Article 49 of the EC Treaty outlawed restrictions on the freedom to trade across borders, Article 46 of that Treaty permits exceptions to it.
"[Article 46] allows restrictions justified on grounds of public policy, public security or public health," said the ruling. "A certain number of overriding reasons in the public interest which may also justify such restrictions have been recognised by the case-law of the Court, including, in particular, the objectives of consumer protection and the prevention of both fraud and incitement to squander money on gambling, as well as the general need to preserve public order."
"In that context, moral, religious or cultural factors, as well as the morally and financially harmful consequences for the individual and for society associated with betting and gaming, may serve to justify a margin of discretion for the national authorities, sufficient to enable them to determine what is required in order to ensure consumer protection and the preservation of public order," it said.
The Court said that though countries were allowed to make exceptions to free trade laws for gambling, they had to make sure that they could justify their decision.
"The Member States are free to set the objectives of their policy on betting and gambling according to their own scale of values and, where appropriate, to define in detail the level of protection sought. The restrictive measures that they impose must, however, satisfy the conditions laid down in the case-law of the Court, in particular as regards their proportionality," the ruling said.
The ECJ said that it was up to a country's courts to judge whether or not a government's laws on gambling are justified and proportionate. The Dutch court had ruled that they were.
Betfair argued that it should be able to operate in the Netherlands using its UK licence, but the Court said that this was not true in the case of the online betting industry.
"It should be noted in that regard that the internet gaming industry has not been the subject of harmonisation within the European Union," said the ECJ ruling. "A Member State is therefore entitled to take the view that the mere fact that an operator such as Betfair lawfully offers services in that sector via the internet in another Member State, in which it is established and where it is in principle already subject to statutory conditions and controls on the part of the competent authorities in that State, cannot be regarded as amounting to a sufficient assurance that national consumers will be protected against the risks of fraud and crime, in the light of the difficulties liable to be encountered in such a context by the authorities of the Member State of establishment in assessing the professional qualities and integrity of operators."
The Court said that a previous case involving online gambling firm Bwin had established that internet betting was inherently more risky for consumers than shop-based betting.
"Because of the lack of direct contact between consumer and operator, games of chance accessible via the internet involve different and more substantial risks of fraud by operators against consumers compared with the traditional markets for such games," it said.