Choice of pain meds could easily encompass an hour-long lecture, but generally I consider the severity of pain (potency of drug), route of administration (do I want IV or po, through a PCA), half-life, side-effects, route of elimination, allergies, and cost. There are more esoteric reasons too. You'll often see outpatient clinicians use lots of vicodin or similar meds (like Zydone, Lortab, or other such preparations of hydrocodone plus acetaminophen) because they're not "schedule II" meds, and thus can legally be called in to a pharmacy, and can be written for refills. More potent, highly-regulated opioids such as oxycodone or oral morphine tabs, or even methadone, need to be written in hard copy form, and cannot be refilled, so they're more of a pain to manage in the outpatient setting.
Sometimes I choose pain meds based on staff comfort with their administration too. This sounds silly, but if you order 7-10mg of morphine IV for acute pain you're likely to get a page from an uncomfortable nurse. Interestingly, order 1mg of dilaudid IV and they won't bat an eyelash, despite the fact that this is about equivalent to 7.5-10mg of morphine! The potency is different, but as long as you dose the medication appropriately you could use either one...it's just that 1mg seems much safer to many people compared to 8 or 10mg. From some ER studies though, the effective dose of morphine for acute pain is about 0.01mg/kg, so for a typical 70kg person 7mg of morphine is appropriate (though you'll often see 4mg IV ordered as a reflex...it often doesn't do much, not surprisingly). Also, morphine tends to have more autonomic effects, such as hypotension from vasodilation, which you may want in the case of chest pain/CHF, but may not want in the case of shock/sepsis, etc. It also tends to cause more side effects like itching, as mentioned above, and it's renally cleared, which is bad news sometimes. Its metabolite, morphine 6 glucoronide (if I spelled and remembered that correctly), can actually precipitate seizures, so you really shouldn't use IV morphine in renal failure patients, or should do so cautiously at least. I also never really use IV fentanyl since it's so short-acting, but this makes it great for ICU sedation/analgesia with a drip, since you can turn it off quickly, and also great for procedures like colonoscopies or EGDs, and acceptable for use in a PCA too (often seen post-op). But given its short half-life, a single IV dose on the wards for acute pain is kinda silly.
You also want to consider whether this is for short or long-term use. If someone has orthopedic surgery, you wouldn't typically start them on long-acting oxycodone (like Oxycontin), whereas if they have had 20 years of severe low back pain from an auto accident, with multiple surgeries, etc., or have pain from cancer, you would more likely start a long-acting medication. This gives you "smoother" pain control, without lots of peaks and valleys, thereby maximizing the pain relief and minimizing the symptoms of oversedation vs. inadequate analgesia. This is why methadone can be so great for chronic pain, since it's so absurdly long-acting, but it's also dangerous because it has QTc prolonging effects, and can cause fatal overdose and/or arrhythmia if not monitored and dosed very carefully.
Phew...that's a lot of info, and there's lots more to know too. But hopefully that's a helpful start for you!