You buy from the developer or the previous owner.
> And surely if you own them, they have no legal right to tell you want to do unless it breaks the law?
Membership to the HOA is required by and included into the building sale, contracts are binding.
[0] http://en.wikipedia.org/wiki/Covenant_%28law%29#Covenants_re...
[1] http://en.wikipedia.org/wiki/Covenant_%28law%29#Covenants_in...
So why in the hell would you sign that sort of deal in the first place? Well, if you don't have to, you shouldn't. But in some cities, such as SF, it's extremely hard to buy a condo that doesn't come with an HOA.
HOAs are as prevalent as they are because in most cases they are highly beneficial, both legally and financially, to building or subdivision developers. So more and more developers are using them.