Working out isn't easy. If it was, they would call it something else.
In that spirit, let's look at why and how a lot of injuries happen in the gym. Often, people don't listen to their bodies and try to push past warning signs. Then they end up injured, frustrated, and feeling bad about themselves. Other times people use the wrong muscles/joints to perform movements and end up putting excessive strain/force on places that can't handle it - boom, there goes your hamstring. And occasionally someone will smack themselves with a heavy object or fall over. I'm going to go out on a limb here and say that all of these situations (even the last one) is largely preventable by focusing on two aspects of movement: attention and a proper warmup.
Everyone pays some degree of attention while working out, of course. But there's a difference between paying attention and being attentive. If you're talking to your friend while you're lifting, you're not being attentive. If you're thinking about what you're going to make for dinner while you're doing goblet squats, you're not being attentive. Whenever you're moving, try to think specifically about what you're doing...for instance, with the goblet squat, think "Weight in the heels. Shoulder blades engaged and dropped. Push up with the glutes and bring the tailbone underneath at the top. Keep the bellybutton pulled in. Breathe." If that seems like a lot to think about, that's because it is. Moving your body in complex ways requires complex thought - if you're not checking in on all levels, you're not getting the most out of your movement, and are at risk for tweaking your knee while you're spacing out! As a trainer, it's my job to not only be that voice for my clients, but also help them develop that nuanced voice for themselves. You know, for all those times when I'm not yelling at them. :)
So what's a proper warmup? I'm sure you've heard by now that it's important to get the blood circulating before you do a ton of stretching or big movements. A few minutes on a rowing machine or jumping rope or some cardio equivalent is always a good start. After that, though, the kind of warmup you do should be determined by the content of your workout...in other words, your warmup should be as specific as possible to the work you're going to be asking your body to do. For instance, if you're going to be doing a lot of leg/core work (squats, deadlifts, lunges) I would do some kind of full body check-in, a dynamic warmup for the quads/hamstrings, and something like inchworms to integrate the upper and lower body. I'd also do some mid-back activation to get the spine warmed up and ready to support weight. What I WOULDN'T do is a lot of pushups and pullups or other upper body exercises. When in doubt about whether to do an exercise as a warmup, ask yourself, "How will this help me prepare for what I'm about to do?" If you can't answer that questions, skip it and find something else that will serve you better. Pay special attention to areas where you struggle - if your hips tend to be super tight and you know you're going to be doing Windmills in your workout, try a hip opener like this or this to get your range of motion opened up.
If you're warmed up appropriately and you're being attentive during your workout, you should be able to move your body in the best and most pain-free ways available to you, and minimize your risks in the gym. :)