To understand the origins of geothermal energy, you would have to go deep beneath the surface of the earth and travel back millions of years in time. The formation of the earth is a mystery which hasn’t been completely solved by scientists even now, but we certainly have an inkling of what happened at the beginning. The earth was basically a spinning ball of fire which cooled down gradually but since we are talking about astronomical proportions here, the time required to cool is not in hours, days, months or even years but in millions of years. Hence the net result is that even today the earth has not completely cooled down but is hotter at it center than the surface.
In fact the word geothermal is made out of the combination of the words geo and thermal which stand for the earth and heat respectively. Hence geothermal energy is the energy present within the earth trapped as heat.
The diagram below gives a dissection of the earth and different layers. As you can see the outermost layer or the crust where we live and survive is nothing more than a thin layer as compared to the other layers. The temperature of the earth rises towards its center and the source of heat at that depth is the continuous decay of the radioactive materials which are releasing heat. There are other sources of heat generation as well such as the effects of the acceleration of the Earth.

click to enlarge
This heat does not have a direct outflow at the surface normally speaking, but there have been diverse manifestations of this phenomenon since times immemorial in the form of volcanoes, hot springs, etc.