Lionel Ainsworth scored the only goal as Motherwell rode their luck to beat Aberdeen 1-0 and move second in the Scottish Premiership table.