If it's important to say how long they had to wait, then you need to use this form.
Note "finally"? It means they were impatient for the bus, so it's important to say how long they were waiting.
"They were talking when the bus finally arrived. (is it correct?) BUT! They had been talking for ten minutes when the bus finally arrived. ?"
Yes, both sentences are correct. You got it. But you maybe wouldn't use "finally" in the first sentence.
How about this?
"They had been talking for only ten minutes when the bus arrived. ?"
See? Now the emphasis is on how soon the bus arrived, a different meaning but the same form.