Oh man I could write a thesis on this lol. Tbh, that's probably the thesis I would've written about if I had the chance to finish my career.
The TL;DR is gut microbiome and epigenetic adaptation for the most part. The other factors are socioeconomic, genetic, and neuroplasticity. Gonna talk about microbiome because I'm seeing most people usually know something about the rest of them but not this one.
Our gastrointestinal system actually has its own division that sends updates to the brain, and the little bacteria and other microscopic critters we got in our guts tell the brain what they need so they eat too. We need them because they help us break down our food, our body can't do it alone.
We come with a little starting pack of bacteria/microbial critters in and on our bodies from the moment we're born. That little pack will help us get our nutrients for the rest of our lives. But it's like an RPG; what you eat is gonna give the pack different stats to invest in, as some cell types will thrive while others starve.
So they talk to our brain. Not only do they tell us what to crave, they brain adapts by changing what our taste buds pick up to get enough to feed both us and them. Overweight people have been found to need a higher count of sugar to taste it compared to underweight people, for example.
The easiest ones to feed are the ones that thrive on sugars, unrefined carbs. Sugar gets used up reaaaaalll fast, so you get hungry more often in part cause they eat it fast, too. Then they multiply and start asking for more.
We also got some who help us break down fats and fibers. Those take a real long time to work on, so they spam the brain asking for food less often.
And over time, the body will adapt to these trends. Our body records and memorizes the base weight (fat + muscle distribution) we've been operating optimally on for a while and tries to keep it in that range, reinforcing cravings. That's why fad dieting is pointless if you're not gonna do that shit for LIFE. It is a marathon.
Main issue is we'd deplete all our natural resources if everyone on Earth ate ideally. I'm not joking, we'd cause mass extinctions of a bunch of aquatic species if we did in as fast as a week I think.