I struggled with it a lot as a child. These days I use a filter of sorts. If I am with my friends, I assume they are joking the majority of the time. If I am at work, I assume there are no jokes the majority of the time. But the most important tool has been asking myself if I thought someone was intentionally being malicious.
If I think someone is not intentionally being malicious, or has no apparent reason to be malicious, I assume the joke was meant with good or playful intentions. Even if the content of the joke was hurtful. Because I know exactly what its like to say something that hurts the feelings of others without intending to.
If I think someone has a reason to be malicious, then I take the joke as an active attempt to hurt my feelings or pull one over on me via word games, and respond appropriately.
So basically, context is incredibly important for me.