That may be commonplace in the U.S where states compete for business with each other, but it's actually banned by the Treaty of Rome, i.e one of the founding documents of what would later evolve into the EU. If a company sets up in the country then they're supposed to have the same tax rate apply to them as every other company that's set up in the country.
At the time the Irish corporate tax was 16% (now 12.5%), but Apple got a special classified deal that gave them a tax rate of 0.05% and Ireland being in the EU common market they then routed all of their sales in the EU trough Ireland with this deal. Thus with the help of this classified deal, which was later leaked to the media, Apple was allowed to make profits in the billions in the EU and pay next to no tax on this profit.
As for what happened here Apple's extremely pricey lawyers were able to muddy the waters enough that there wasn't an absolute certainty in the eyes of the court that this gave Apple an absolute advantage over their competition in under what the Treaty of Rome prohibits. In other words they muddied the waters and convinced the court to apply a very narrow interpretation of the government assistance rules in the Treaty of Rome.