My general approach is to say "it's been really great talking to you" or "catching up" or "hearing all about this". I do NOT then add ", but I have to go now." I just say that part and pause. Most of the time, the other person stops - after all, you've just indicated that the topic has successfully completed. Sometimes, they change the subject. Many times, they say "wow is it 8:30 already I can't believe we talked so long! I'd better go!"
If they just keep right on steamrollering - "yeah, it's great, you know another thing that happened at the grocery stores that really steamed me was - " you can say "I would love to hear that story another time." Again, don't say "but I have to go." If you're conversing in person, you can simply walk away from the conversation at this point. "Great seeing you!" and off you go -- to your car, out of the office, to the kitchen or bathroom of the party you're both at, to the other side of the dance floor, whatever.
If you're on the phone, or talking in person in a situation where you can't leave (you live with them, for example, or you're waiting for a bus or your turn somewhere) then it's harder if they don't pick up that you're done, because you can't just walk away. In person, you can say "I have to do this" and wave your phone, then deal with some email or whatever. On the phone, you can interrupt them "hang on a sec" and then after a pause say "oh man, I'm going to have to go. Talk soon!" Neither of these involves lying though they are deceptive.
If you can't or won't leave, then you need to change the subject. So you can again give a positive summary: "It's been great hearing all about your trip" or "I'm really impressed with how you're holding up under all that stress and effort" in a tone that would normally lead to "but I have to go now" and instead hold a significant pause and then say something that starts a new topic. If you're willing to hear more from them on another subject, ask them a question like "what are you going to do to relax when it's all done?" or "how is [someone else you both know] doing?". If you really feel that it's your turn to talk, try "Did I tell you about my trip this summer?" or "my big fight with the water company?" or "my plans to redo my backyard?". Or "can I ask for some advice on [my vacation plans, my Christmas shopping, living room furniture]?"
If you can't or won't leave, don't want them to talk about something else, and don't want to talk about anything yourself, you're in a very hard position. You will simply have to tell them what you would rather do than listen to them any more. You can again start with how great it's been to chat, and then pause and without saying BUT, add "If you'll excuse me, I must/have to/need to sit with my thoughts for a little while" or "finish reading this" or "catch up with something I had planned to do while waiting here" (this last one works best with complete strangers eg while on a bus ride or at a doctor's office, and not at parties or family dinners.)