God opened the mouth of the donkey to allow it to teach Balaam a lesson in humility and seeking to be in tune with the ways of the Lord. There was an angel there, and Balaam was being too hard headed to know it.
"22:30 And the donkey said unto Balaam, Am not I thine donkey, upon which thou hast ridden ever since I was thine unto this day? was I ever wont to do so unto thee? And he said, Nay."
...thanks.
(P.S. I had to swap the word 'a.s.s' for donkey... as YA censored it ;0)