Short answer. Simple answer. Most websites primarily buy lists from phone companies and scrape the web. Sometimes they get lists from Chambers of Commerce registries, license registries, Post Office, associations, and so forth. As well, they can buy lists from rating companies like Dunn and Bradstreet and various lower level marketing companies which often just sell data in bulk. Some significant databases can be purchased for just a few hundred dollars.
In a prior life, I purchased data and built databases for customers and my own business so I can testify to the above. There are many other sources of data that are a bit more obscure, but the ones above are more likely.
As for Yelp and Google Business, I cannot tell you specifically. These are slightly different than most. I would assume that Google uses their search index more than anything and Yelp purchased and amassed a database.