This is a question I get a lot.
If you have had an obvious injury, use ice right away, and for the next couple of days. If there is inflammation, bruising or swelling, only use ice until it subsides. Apply an ice pack for 20 minutes using only a thin layer (like a tee shirt) between the ice pack and the skin. Do this several times a day.
If you woke up with stiffness and discomfort, for example, use heat. Heat, especially moist heat, is better for chronic aches, pains, and stiffness. Moist heat penetrates into the muscle better than dry heat, but both are beneficial. I would use heat for no more than 20 minutes at a time, but it can be reapplied every hour or two. Neck pain, middle back pain, and lower back pain can all benefit from heat and gentle stretching exercises.