Reddit often asks: if the Decepticons hate Earth so much, why not just leave? The answer varies by story, but the reasons are similar.

Energy

Earth is rich in energy — oil, electricity, sunlight and in some versions Energon deposits. For an army whose home planet is drained, it's the most valuable resource in the galaxy. In the classic cartoon almost every Megatron plan is a way to take Earth's energy and ship it to Cybertron.

Artifacts

In the films the main reason is the AllSpark, the Matrix, ships and technology that ended up on Earth. Whoever holds them can revive Cybertron or win the war.

War with the Autobots

As long as the Autobots are on Earth, the Decepticons can't leave: otherwise their enemies would grow stronger and return. Earth became a new front in the same old war.

Pride

Finally, there's Megatron's character. For him retreat would mean defeat. So the Decepticons stay not because they like it here but because leaving would mean admitting the war is lost.