Declines or mid‐elevation peaks in invertebrate diversity with elevation are often attributed to climate and geometric constraints. However, vegetation structure may also drive diversity patterns, especially for tree‐dwelling species, via its effects on microhabitat use and competitive interactions. Here we investigate these effects on the diversity and community structure of tree‐nesting ants over elevation. We exhaustively sampled ant nests in 1254 trees within continuous plots of primary rainforest at low (200 m a.s.l.), mid (900 m a.s.l.) and high (1800 m a.s.l.) elevation in Papua New Guinea. Ant diversity, nest abundance and tree occupancy peaked at mid‐elevation. Although host tree diversity also peaked at mid‐elevation, there was low specialisation of ant species to tree species at all elevations. Mid‐elevation trees hosted more species, more nests and a greater diversity of nest types than trees of a similar size at low or high elevation. Tree size and nest microhabitat use were the strongest predictors of species composition, explaining twice as much of the variability in the communities than elevation. At mid to high elevation there were proportionally fewer large nests than in the lowlands, with an increase in smaller nests in live hollow twigs and epiphytes. There was high species turnover between elevations, and between trees within elevations. Species co‐occurrence patterns within trees differed with tree size, and with elevation. In large trees species tended to co‐occur at random at low and high elevation, but co‐occurred more often than expected by chance at mid elevation, indicating an elevational shift in competitive interactions. We conclude that the more extreme diurnal temperatures at higher elevations, combined with increased epiphyte availability, drive ants to nest in more insulated microhabitats. This results in smaller colony sizes and a decrease in interspecific competition, thereby boosting species co‐existence at mid elevation.