Group selection may be defined as selection caused by the differential extinction or proliferation of groups1, 2. The socially polymorphic spider Anelosimus studiosus exhibits a behavioural polymorphism in which females exhibit either a ‘docile’ or ‘aggressive’ behavioural phenotype3, 4. Natural colonies are composed of a mixture of related docile and aggressive individuals, and populations differ in colonies’ characteristic docile:aggressive ratios5, 6. Using experimentally constructed colonies of known composition, here we demonstrate that population-level divergence in docile:aggressive ratios is driven by site-specific selection at the group level—certain ratios yield high survivorship at some sites but not others. Our data also indicate that colonies responded to the risk of extinction: perturbed colonies tended to adjust their composition over two generations to match the ratio characteristic of their native site, thus promoting their long-term survival in their natal habitat. However, colonies of displaced individuals continued to shift their compositions towards mixtures that would have promoted their survival had they remained at their home sites, regardless of their contemporary environment. Thus, the regulatory mechanisms that colonies use to adjust their composition appear to be locally adapted. Our data provide experimental evidence of group selection driving collective traits in wild populations.