A long-standing puzzle in ecology is coexistence of many species despite relatively few limiting resources. Studies using competitive community models have found that temporal environmental stochasticity (TES) can provide a solution by providing a rare-species advantage, for example by creating temporal niches. However, this appears to contradict studies using population models, which have found that TES-induced temporal fluctuations in species abundances increase the chance of species becoming rare and hence increase their extinction risk. Here, we clarified how TES affects species richness by analysing a competitive community model using a novel mean-field approach. We found that when temporal correlation in TES was weak, the dominant effect of TES was to create temporal niches by decreasing the strength of interspecific competition via a temporal averaging effect, thereby increasing species richness relative to the neutral case without TES. In contrast, when temporal correlation was strong, the dominant effect of TES was to increase the strength of selection and hence interspecific competition, thereby decreasing species richness. Compared with these indirect community effects of TES, the direct effects of TES on species richness via temporal fluctuations in abundances of species populations were relatively minor.