That's empathy, in a nutshell.

To summarize:

Empathy is the art of seeing the world from someone else's perspective. Empathy allows you to understand what the other person is thinking and feeling, and understand why they make the decisions that they do. It's useful stuff for social interaction, because the better you understand people, the better you can build a relationship with them.

To improve your empathy, first learn how to empathize with yourself. Get in touch with your emotions--learn what makes you mad or sad or whatever, and how it feels to be feeling different emotions. Give yourself permission to feel. If you bottle up your own feelings, it will make it very difficult to understand the feelings of another.

Next, ask yourself "How does this situation appear to the other person?" during every interaction--and spend the necessary time to think of a reasonable answer.

As you continue to get in touch with your own emotions, and as you continue to take the time to explore the perspective of others, your ability to understand what others are thinking and feeling will steadily increase. You will find it easier to avoid inadvertently offending others, and you will find yourself picking up on subtle shifts in emotion--noticing, for instance, when a normally ebullient friend seems strangely sad and subdued.

There's no shortcuts or tricks to this. But if you invest the time, you will find yourself richly rewarded.  Good luck!