I'd guess the answer is, you open your mouth and utter when you have something to say. And that something to say is in response to another stimulus - your emotions. Even if it's a simply 'good morning' type of exchange with your neighbour or the postman, that's an emotional response - you want to be friendly or polite.
Of course, quite a lot of our dialogue in real life is about being friendly or polite, and there's also the desire to be helpful, to give information but some of it will be fuelled by stronger emotions. Jealousy. Anger. Love. Fear. Whatever the emotion may be, I'm pretty certain that you're not randomly opening your mouth and letting a stream of consciousness pour out. You have reasons for speaking.
Same with written dialogue. It's fuelled by emotion. Once you know what the characters emotions are, you'll know what they have to say. And because it's fiction, and not real life, most - all? - of the situations we show our characters in are going to be subject to the more powerful emotions. Bertie Wooster may be written to amuse and entertain, but for Bertie the absurd situations he finds himself in are often fuelled by fear. It's funny to us, real to him.
When writing dialogue I find it best not to think about what they say. Instead I think about why they're saying it, what emotion is driving it. And then, what they say just comes.