I've struggled with a similar problem. I like to make my world immersive and give distinct features to each NPC that my group meets. However, this can cause players to talk to an NPC for 10 minutes to buy a meal.
As long as everyone is having fun, this is not a problem. However, as you have pointed out, some players may be bored with prolonged dialogue. So, what I tend to do is to do a role play when first meeting with an NPC. Give the guardian of the tavern a personality when he enters. Once the character is established, I will not interpret the scene unless the players try to do something interesting with the conversation. Instead, I simply declare "you are going to buy a meal at the bar, it charges you 3cp".
If the conversation is banal, do not bother. Keep your dialogue deep and long scenes for important things. This has the added benefit of reducing the number of distinct characters you need to play / remember.
If a conversation is important and you want to play it at the table, you can still use similar techniques.
During the conversation, if the PCs have gathered all the important information from the NPC, just tell them:
"You spend another 15 minutes talking to the sailor without learning anything of interest, except that he's wearing a siren's tattoo in a place that he will not show you."
When talking to a powerful NPC, a lord or a leader, you can say things like:
"The king seems visibly frustrated with your questions, you recognize that you have more than one or two before telling you to leave"
If they do not understand the hint, or if it's too aggressive, remember that NPCs are also people and that they probably have something else to do.
"I'm afraid I have to go there, never enough hours in the day for everything I need to do"
Then just let the NPC go.
You can also ask another NPC to interrupt them. If the NPC is a trader, he will have other customers, the peasants will have children or friends, even the guardian could come forward to accuse them of loitering.
What about when the NPC has no reason to end the conversation?
Sometimes you will find yourself in a situation where none of the above techniques will make sense. The hermit in the marsh who only has time to talk to adventurers for example. In these situations, you can take the opposite approach, inducing the characters to end it themselves.
PC: "How long have you been here?"
Hermit: "Well, I arrived for the first time on a lunar night … the swamp was beautiful …"
DM: The Hermit launches a description of his arrival in the marsh in 20 minutes.
PC: "But how long is it?"
Hermit: start the same story …
With this approach, you need to clarify the passage of time. The PCs burn the daylight by talking to the NPC. This means that they may not have the time to go to the important MacGuffin tonight.
How to know when to pass the hand
You mentioned in a comment on another answer that it was something you were fighting with. I will try to give you some tips.
On the leadership side, it's pretty easy, if the party has gathered all the information you intend to share with it, it's safe. Although you should be careful not to steal the entertaining conversations and the entire table.
For example, I had a very Australian NPC (I'm Australian too and I slipped with my accent when I introduced the NPC) that the party found hilarious. His goal was to give them directions to this important part of the city. However, I let the players continue to talk to him until I miss Australian jokes. So I let him go back to work.
The hardest part is when you know that there is still information to collect but that the players have begun to interrupt the conversation. It is difficult to give advice because it varies from one group to another, but you have to watch the players: players leaning back in their chair, players who do not participate in the conversation, phones or other distractions to study, and a reduction of the role play of effort.
At this point, you can reduce the conversation to a roll of dice. Have them make a social assessment and base the CD on the quality of the conversation. If successful, you give them all the important information contained in the summary transmitted by hand. In case of failure, they can get it only a part, if any.
You want to use the fiat and GM hand to indicate that the conversation has continued but that they had all the important information. The player must have confidence that you gave him everything he could have gotten through a continuous conversation.