There are many ways in which musicians get paid. For advertising music in particular, aside from upfront fees, most of the time back-end royalties and other residuals can be collected.
Below are links to a few resources for further reading.
Publisher and Composer Royalties Collection Societies:
Performance Royalties Collection Society (digital only):