Just say "Hey, I gotta run. It was nice talking to you." Or something to that effect. Most people aren't going to get upset if you need to wrap up a conversation. And if they do get upset (assuming they aren't having an emergency), it's a red flag.
Alternatives: "can we talk later, I'm really busy with ___."
Or in a work setting, "I'm in the middle of something, do you mind sending an email?"
Social settings, excuse yourself to the bathroom, they'll move on.
If you don't like the conversation, "do you mind if we talk about something else?".
There are tons of ways to wrap up or avoid conversation. The more honest you are the better. And take note of how others gracefully end conversations with you and use those phrases too.