There is a lesson learned by Bilaam’s persistence to go against Hashem’s wishes, and attempt to curse the Jewish people. It seems crazy that someone would have the audacity to go against the King of the Universe!
The Talmud in Masechet Makot, comments on this phenomenon. We are told that the path that a person chooses to take, is where he will be led.
If one chooses the path of righteousness, he will be helped from Above, to achieve this goal. He will have holy people become part of his life, to help in his quest to get closer to G-d.
On the other hand, if one seeks wealth and fame, as Bilaam did, he will also be led in that direction. If his motivation is selfish, this will not end well. Either he will hit insurmountable snags on the way, or he will be given the wealth that he seeks. However, this wealth will torment him, and give him no rest. He will be suspicious of everyone that they are after his money, and he will learn that money does not buy happiness.
This is the main explanation of free will. We make the choice as to the direction we wish to take in life. We will be led in the direction of that decision, good or bad. In Bilaam’s case, it certainly did not end well for him.