How does an advertising online business work?

I’ve been curious to know the inner mechanisms of an online advertising agency. What do they do? They pay anyone who put one of the company’s ads on their website? … How do they get clients? In sum, how do they work?
Thank you for your help.
So companies lick ValueClick and ClickAgents work the same way?