As to whether every company buying ads is making a good investment, mileage may vary - but the blunt answer to your question is that yes, people do purchase things because they saw ads for it, the advertising economy is well understood. Companies like Google whose fortunes rest almost entirely on the known efficacy of advertising are not full of idiots who have never thought about whether or not ads actually work.
"Is an economy based on selling attention ultimately the most beneficial and productive one for all participants" is a separate question, but it's not the question you're asking.