A common question - and surprisingly difficult to answer. To start with, it's a question of realising that each has two quite different effects.
Both cold and warmth (or even moderate heat) act as pain 'blockers' - in other words, rather like rubbing a stubbed toe, they 'distract' the brain from feeling the usual pain or ache. But both do something else too.
Cold can seep through the skin, and serve to slow down inflammatory activity and blood flow in a painful area. This will work when there actually is inflammation causing the bulk of the pain, which is mainly only in the first few days of a new injury. A good example of an injury like this is where you have stretched a joint too far; the joint then 'seeps' inflammation (which causes pain and can cause a 'hot' feeling) over the following few days.
Warmth can relax the muscles and allow blood to flow through better. This is especially useful when muscle tension itself is causing the ache or pain, and there is no inflammation.
A neat way of remembering all this is to think: warmth is good for me when movement makes me feel better, and cold is good for me when I'd rather not move because of the pain!
Summary: both cold and warmth (or moderate heat) have different and similar effects! And you can use both in different places at the same time - for example, a warm water bottle on an achy muscular area on one side of your back, and a cold source on a hot sore other side.