Uh. Usually a mix of context and tone. There are of course situations where someone says something awful and when they get a poor response, just say "I was only joking" which they were not, they just don't want consequences for being awful.
Depends on who you are talking to when your joking around, certain friends you tell them something funny/joking but you have this look on your face that tells them your having fun. Someone I dont know i might phrase at the start that reminds of something funny or you say something so absurd then grin so they know your not serious.