Linking phase transitions and quantum entanglement at arbitrary temperature