There is not a simple, straightforward answer for this, but for some basics:
In general, ostomies are created to either protect a more distal anastomosis, to allow for a functional GI tract when a more distal anastomosis is not safely possible in an acute setting, or to all for a functional GI tract permanently in someone who doesn't have an option left for restoration of GI continuity.
Most patients undergoing emergent colectomy (ie for diverticulitis or acute obstruction) will get an ostomy. This is usually a situation where the GI tract is left entirely in discontinuity due to severe inflammation in the affected area. The ostomy (often colostomy unless the resection is proximal) is placed temporarily and the distal stump stapled off such that the inflammation has several months to die down and you can return to the abdomen to reconnect the GI tract at a later date. Or in case of obstruction, often the upstream bowel is so dilated you can't successfully/safely make an anastomosis to the normal caliber bowel more distally in the acute setting.
In case of very low anastomoses, we often create a diverting loop ileostomy more proximally, as the main feared complication of this is a leak, and the main cause of a leak is tension on an anastomosis. As you can imagine, anastomoses down in the pelvis tend to be more difficult, including in terms of reaching without tension, and therefore a diverting ostomy gives that anastomosis time to heal. Then the DLI is reversed at a later date when healing is complete. Often for right hemicolectomies, or sigmoid colectomies in the elective setting, we will not create an ostomy as there is less likelihood of leak.
Some patients have Total proctocolectomies or APRs and have no distal options for anastomosis. Total proctocolectomies technically can get a J-pouch, and coloanal anastomoses are also possible in general, but as you can imaging, these can be lifestyle limiting in that patients may have multiple bowel movements throughout the day, issues with poor continence, etc. Some people find it easier to continue to live their normal lives with an ostomy, and elect to have a permanent ostomy. Or in cases where we have exhausted all distal anastomotic options, a permanent ostomy would be placed.
This is a very, very general answer to your question but hopefully it gives you a foundation to work with.