How do musicians write tracks together?

How do musicians write tracks together? And why? For various reasons.
Sometimes two people actually write faster. Alone, a musician worries about the quality, overthinks everything for ages, and constantly winds themself up with the question: what if it sucks? Working together, the second person can calm them down with the phrase everything’s alright and the work goes 10 times faster.

Sometimes someone has written a draft, but doesn’t know how to develop it and another musician has heard this draft and says that they can help—let’s do a collab.

Sometimes the strengths of one cover the shortcomings of the other. For example, one musician knows the rhythm, and the second the melody.

Sometimes one musician doesn’t understand the equipment at all, but has a lot of ideas and, most importantly, has a vision of the whole track with its sound, so they give this information to someone savvier.

Sometimes musicians write well separately, but together they form a superpower that moves mountains.

And sometimes a musician claims another’s work as their own. It happens in different ways.