When you are trying to remove inflammation from an area I.e. twisted ankle, you need to cool it as much as possible. That reduces the spread of inflammation and this includes reducing blood flow. Do that for a day or two after an acute injury or surgery, maybe longer to prevent inflammation. Once the tide of inflammation is under control, you can use heat to get that blood flow back and to allow removal of breakdown products and to bring in more blood to heal. There are situations where you will alternate between hot and cold but usually not with an acute injury.
Heat increases bleedflow and can help relax muscles
Bodies are complex, there's a lot of systems all connected together which means injuries often have multiple aspects to what's wrong.
Probably some of the most common injuries you'll see this kind of treatment for are things like sprains and strains, where you've stretched muscles, tendons, or ligaments a bit too far and maybe tore them a bit. Ligaments are tissue that connects your bones together, and tendons connect muscles to your bones.
Your body responds to this by swelling, it sends extra blood to the area to start working on repairs. It may also help somewhat to cushion and/or immobilize the joint slightly to prevent you from making it worse. Heat opens up blood vessels and gets even more blood flowing through the area, so it can help speed up the healing a bit, and if it gets your muscles to relax it can take some of the stress off of whatever's injured, don't want your muscles pulling on some damaged ligaments too much, you want them to relax so they can heal.
But that swelling can be pretty damn uncomfortable on top of any pain from the actual injury itself, your skin, muscles, and other tissues can only stretch so much to accommodate that extra blood so it's putting pressure on everything around it. Cold sort of slows down your nerve cells so they can't send as many pain signals to your brain. And like how heat opens up your blood vessels, cold makes them contract so you don't get as much bloodflow and so less swelling.
So you can see that both have their benefits and drawbacks, overall cold is more of a pain management thing, and hot is more to expedite the healing, and depending on the type of injury, how much pain you're in, etc. it can be beneficial to do one, the other, or alternate between both.
This is sort of an ELI5 answer, im sure there's a bunch of special cases or ifs, ands, and buts that could be sprinkled in there, but I'm no medical professional, but hopefully that gives you the general gist of it. If anyone spots anything I got wrong, please let me know.