The nominal answer is that HOAs are membership associations to which owners in a condo building or neighborhood are sometimes obligated to join. The homeowners pool dues every month to cover things like garbage collection, building maintenance, etc. The HOA also acts as a governing body over the building's residents, and is free to impose any number of rules and restrictions on usage, design, etc., in anything defined as a "common area" (chiefly, any part of the building that is outside the airspace of the condo interiors). Some HOAs set additional restrictions on what you can do inside your condo, provided that activity, construction, design, etc., affects the neighboring units in any way. Above and beyond their common-area jurisdictions, many HOAs are free to establish "house rules," which are arbitrary and binding restrictions on just about anything that the HOA board happens to dislike.
The cynical answer is that HOAs are legal fictions set up to shelter building developers from the financial burdens and liabilities of having to pay for, or be on the hook for, any of those services or maintenance needs. HOAs are the greatest invention in favor of real estate developers since the timeshare.