This a very common question and like many things to do with the human body there are some simple answers that suffice in most cases but some complexity if you dig a bit deeper.
Ice can numb small nerve endings and relieve pain. It may also decrease the small amount of bleeding that occurs in the first day or two following an injury like a muscle tear or ligament sprain. Ice is best used in the first few days following an injury but can be used other times as long as it is giving you pain relief.
Heat relaxes muscles, improves circulation and soothes aches and mild pain. Heat should not be used in the first few days following a muscle tear or ligament sprain as it can increase inflammation and swelling. Most people find heat more comfortable than ice and it may make you feel less stiff. In some cases when pain is very intense, heat doesn’t always give enough pain relief to everyone. In these cases you can try ice as long as the ice doesn’t make the pain more intense.
To summarize, if you feel you have actually sustained a new injury like a muscle tear or ligament sprain that is producing a lot of inflammation and swelling, only use ice and avoid heat for the first few days. Otherwise use either heat or ice, which ever gives you the most pain relief and allows you to move easier. Everyone is different so do what works best for you.