It isn't that they know what and when to say something so much as saying it when they think it. Because they are comfortable in the situation, they don't have the overactive mental filter someone with social anxiety has. They don't worry so much about what people will think of what they say, so they say more.
See if that fits with what you observe. If it does, it suggests that the thing you need to work on is removing that filter, so when you do have something to say, you will just say it.
So for one, relax and don't worry about what they might think about you. That's speculative (because you can't read minds) and useless (because you can't change them directly). It's much more useful to think about what you can actually observe (how you and they act), and to work on changing the things that you can directly change (what you think).
Secondly, pay attention to your thoughts and note the times when you could have said something but hesitated. Don't berate yourself for it, just notice it, and notice what you're thinking and what you're afraid of happening that makes you keep your mouth shut. Then when you have some time to think you can ask yourself whether those fears are truly realistic. Most of the time, they aren't. Most of the time, "the worst that could happen" isn't that bad.
I think that if you do this, and focus on getting comfortable with the idea, that you'll find yourself more often having something to say and not feeling that hesitation.