The general rule of thumb is to use ice in the acute stage of an injury (within the first 24-48 hours), or whenever swelling is showing. Ice helps to reduce inflammation and swelling by decreasing blood flow to the area that is injured. The general guideline is to apply ice indirectly (not directly on the skin) for 20 minutes, remove the ice for at least 20 minutes, and repeat as necessary.
Heat is used to increase blood flow, which helps promote pain relief after inflammation and swelling subside. Heat is also used to assist in warming muscles up prior to exercise, any physical activity, or physical therapy.