It's often a voluntary agreement. A developer builds a collection of houses, then sells them under the condition that the owner must join the home owner association set up for those houses. The idea is that the association forces its members to do upkeep, so that the homes don't collectively lose value for being in a "bad neighborhood".