In thermodynamics, thermal energy refers to the internal energy present in a system due to its temperature. The concept is not well-defined or broadly accepted in physics or thermodynamics, because the internal energy can be changed without changing the temperature, and there is no way to distinguish which part of a system's internal energy is "thermal". Thermal energy is sometimes used loosely as a synonym for more rigorous thermodynamic quantities such as the (entire) internal energy of a system; or for heat or sensible heat which are defined as types of transfer of energy (just as work is another type of transfer of energy). Heat and work depend on the way in which an energy transfer occurred, whereas internal energy is a property of the state of a system and can thus be understood even without knowing how the energy got there.
Heat is energy transferred spontaneously from a hotter to a colder system or body. Heat is energy in transfer, not a property of the system; it is not 'contained' within the boundary of the system. On the other hand, internal energy is a property of a system. In an ideal gas, the internal energy is the statistical mean of the kinetic energy of the gas particles, and it is this kinetic motion that is the source and the effect of the transfer of heat across a system boundary. The internal energy of an ideal gas can in this sense be regarded as "thermal energy". In this case, however, thermal energy and internal energy are identical.
Systems that are more complex than ideal gases (such as real gases) can undergo phase transitions. Phase transitions can change the internal energy of the system without changing its temperature. Therefore, the thermal energy cannot be defined solely by the temperature. Thermal energy also cannot be defined by the difference between the internal energy and the net heat transfer in or out of the system, because it is easy to construct thermodynamic cycles where the system begins and ends in exactly the same state, but there is a net flow of heat in or out during the cycle. These cycles can be induced upon rather small generators thus creating a rotor to spin and making electricity. This is referred to as generating electricity.
For these reasons, the concept of the thermal energy of a system is ill-defined and is not used in thermodynamics.
In an 1847 lecture entitled "On Matter, Living Force, and Heat," James Prescott Joule characterised various terms that are closely related to thermal energy and heat. He identified the terms latent heat and sensible heat as forms of heat each affecting distinct physical phenomena, namely the potential and kinetic energy of particles, respectively. He described latent energy as the energy of interaction in a given configuration of particles, i.e. a form of potential energy, and the sensible heat as an energy affecting temperature measured by the thermometer due to the thermal energy, which he called the living force.
I am inclined to believe that both of these hypotheses will be found to hold good,—that in some instances, particularly in the case of sensible heat, or such as is indicated by the thermometer, heat will be found to consist in the living force of the particles of the bodies in which it is induced; whilst in others, particularly in the case of latent heat, the phenomena are produced by the separation of particle from particle, so as to cause them to attract one another through a greater space.