Many US companies come to Ireland for lax regulation and English spoken. They are experts at not paying tax even if there isn't "State Aid". The Apple thing is partly historic because in the days when they were a computer company and making their own computers, they did make some in Cork and they even had Microsoft aid to keep them going (before iPods, iPhones and iPads).
Look at what tax these guys pay in Ireland or Europe (or Luxembourg, Amazon)?
Google, PayPal, eBay, Starbucks, Microsoft, IBM, BSkyB (Irish subs VAT went to UK for years), Analog Devices, the Drugs companies etc.
Apple was using an old no longer appropriate loophole.