Basically ads try to make your mind remember the name of the company, their product and sometimes the music of the ad (which makes you more likely to remember). So when you need something you have a mild chance of remembering that that company sells that product. Also ad personalization exists.