From Chabad.org: Bilaam leaves together with the Moabite dignitaries. G‑d sends an angel with a drawn sword to block Bilaam’s path. While Bilaam couldn’t see the angel, the she-donkey he was riding did, and refused to move onwards, causing Bilaam to strike her. The donkey miraculously speaks, admonishing Bilaam for striking her. Eventually, G‑d “opens Bilaam’s eyes,” and he sees the angel. A conversation between Bilaam and the angel ensues, wherein Bilaam is chastised for his behavior towards his donkey, and again he is reminded only to say what G‑d dictates to him. After this humbling episode, Bilaam arrives in Moab where he is greeted by Balak.

If G-d gave Bilaam permission to go to Balak, why did He send an angel to stop him, and why was He angry that Bilaam was going (Passuk/Verse 22)? Midrash explains that Bilaam woke up early and saddled the donkey himself (unusual) because he hated the Jews so, and was excited at the possibility of being able to curse them. So even though he hadn’t actually cursed the Jews, and even though G-d knew that he ultimately wouldn’t, he was still wrong for intending to do something bad, and that’s what G-d was angry about. Luckily, though, we don’t get punished for improper acts unless we actually commit them.