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.
For these reasons, the concept of the thermal energy of a system is ill-defined and is not used in thermodynamics.