Thermal energy refers to the energy contained within a system that is responsible for its temperature. Heat is the flow of thermal energy. A whole branch of physics, thermodynamics, deals with how heat is transferred between different systems and how work is done in the process (see the 1ˢᵗ law of thermodynamics).
In the context of mechanics problems, we are usually interested in the role thermal energy plays in ensuring conservation of energy. Almost every transfer of energy that takes place in real-world physical systems does so with efficiency less than 100% and results in some thermal energy. This energy is usually in the form of low-level thermal energy. Here, low-level means that the temperature associated with the thermal energy is close to that of the environment. It is only possible to extract work when there is a temperature difference, so low-level thermal energy represents 'the end of the road' of energy transfer. No further useful work is possible; the energy is now 'lost to the environment'.