In the early days (before they became famous) they did indeed write together and collaborate closely on songs. Maybe one was started by John, or by Paul, but then they worked together to finish it. The song "One after 909" on the "Let it be" album was an early song they wrote together as teenagers.
But then gradually they began to write some songs on their own, with the other having little contribution to the song (except maybe in the studio when recording it).
A song like say "Yesterday" was totally by Paul and as he recorded it with a string quartet none of the other Beatles were even on it. "In my Life" was totally by John.
Because they had signed a contract that every song one of them wrote had to be a "Lennon / McCartney" song that is how they were published, even if the other had had nothing to do with it.
Soon it became very clear who wrote what. John wrote All you need is love, I am the walrus, Strawberrry Fields forever, Sexy Sadie and many others.
Paul wrote Let it be, Long and Winding Road, Lady Madonna, Hey Jude etc.
"A Day in the life" on Sgt Pepper was a song they collaberated on. John wrote the start and end but needed a "middle" bit, and Paul already had the "woke up got out of bed" section for a song he was working on, so they joined them together.
The problem came after the Beatles broke up as the writing contract said any song they wrote on their own had to be a Lennon / McCartney song.
So Paul wrote with Linda and all Paul's songs were published as Paul / Linda McCartney, and John wrote with Yoko and all his songs were published as John / Yoko (even though Linda or Yoko probably had little or no contribution).
Paul does comment about this song writing situation with the Beatles on the song "Man we was lonely" on one of his first solo albums when he sings "Singing songs that I thought were mine alone".