Thermal energy of a substance is determined by the movement of the molecules and the potential energy of the arrangement of molecules. Heat transfer will stop when thermal equilibrium is reached. It depends upon the substance how long that takes.