Parshat Balak

What was Bilaam's motivation to curse Bnei Yisrael, and why does Hashem allow him to try?