Be aware of the context and the company you are in. In a professional or family setting, it's obvious that sex - related stories are out of place. In more casual settings, if you sense the conversation is veering towards inappropriate territory, make a joke to lighten the mood and redirect the topic. For instance, 'Whoa, let's not go there! I heard there's a really cool new coffee shop in town, anyone been there?'
One way is to set clear boundaries in conversations. If someone starts to head in that direction, firmly but politely change the topic. For example, start talking about a recent sports event or a new recipe you've tried.