Title favourites Ireland survived a major Six Nations scare as they clinched the Triple Crown by beating a transformed Wales team 27-18 in Cardiff.