Contracts for rights are fiendishly complicated, and many a musician has fallen foul of shoddy deals, and they still do today.
Boradly speaking, the song writers receive the lion's share of royalties, the singer gets a smaller 'performance' royalty, but it really does depend who actually owns the copyrights to any song as to who sees any money from its re-use.
During his lifetime, Neil Aspinall, who oversaw the copyright arrangements for Beatles songs, ensured that no Beatles song ever appeared on a compilation album, or in an advert.
Since his death, both those situations have occurred, so it demonstrates that whomever holds the rights has the say, and receives the money, on behalf of their client, or themselves.
A famous example is the sample of 'The Last Time' by The Andrew Loog Oldham Orchestra, used as a sample by The Verve on their Bitersweet Symphony hit.
The song was written by Mick Jagger and Keith Richard, but the copyright was owned by the notoriously litigious manager and lawyer Alan Klein. Mr Klein licensed a limited sample, the band used more time than was agreed, Mr Klein sued and won, and for years, the band received no royalties at all for their biggest hit.
Since Mr Klein's death in 2009, I believe copyright reverted back to Jagger and Richard, and I believe that have in turn awarded royalties back to The Verve.
As I said, royalties are bound up in contracts, and they are as varied and complex in construction as galaxies in the sky.