We’re not entirely sure how it happened, but some Israelite men fell for some Moabite women, and then, following on from that sin (sex outside of marriage was forbidden):
Numbers 25:2 These women invited them to attend sacrifices to their gods, so the Israelites feasted with them and worshipped the gods of Moab.
They were tricked, we’re left in no doubt about that:
Numbers 25:17 because they assaulted you with deceit and tricked you into worshipping Baal of Peor…
You can see why the issue of eating meat sacrificed to idols was contentious in the early church, several thousand years later.
1 Corinthians 8:4-5 So, what about eating meat that has been offered to idols? Well, we all know that an idol is not really a god and that there is only one God. There may be so-called gods both in heaven and on earth, and some people actually worship many gods and many lords.
But not everyone had such strong faith:
1 Corinthians 8:7 However, not all believers know this. Some are accustomed to thinking of idols as being real, so when they eat food that has been offered to idols, they think of it as the worship of real gods, and their weak consciences are violated.
And this is most likely what happened to the Israelites.