When the Earth formed from a big cloud of dust and gas, it was very warm. As the Earth formed, the bits of dust and gas banged together creating a lot of friction. If you rub your hands together they'll get hot because of friction too. So some of the Earth's heat comes from the friction from billions of years ago when it was forming. There's also a lot of pressure too.
Pressure makes things hot.
Squash a ball and it will heat up. The Earth is being squashed by the gravity that pulls it all into the middle. But the heat comes from another place too - from radioactivity. There are lots of radioactive bits in the Earth and radioactive bits give off heat as they decay. So the Earth is heated up from inside by radioactivity.