Background Gut bacteria might predispose to or protect from necrotising enterocolitis, a severe illness linked to prematurity. In this observational prospective study we aimed to assess whether one or more bacterial taxa in the gut differ between infants who subsequently develop necrotising enterocolitis (cases) and those who do not (controls). Methods We enrolled very low birthweight (1500 g and lower) infants in the primary cohort (St Louis Children's Hospital) between July 7, 2009, and Sept 16, 2013, and in the secondary cohorts (Kosair Children's Hospital and Children's Hospital at Oklahoma University) between Sept 12, 2011 and May 25, 2013. We prospectively collected and then froze stool samples for all infants. Cases were defined as infants whose clinical courses were consistent with necrotising enterocolitis and whose radiographs fulfilled criteria for Bell's stage 2 or 3 necrotising enterocolitis. Control infants (one to four per case; not fixed ratios) with similar gestational ages, birthweight, and birth dates were selected from the population after cases were identified. Using primers specific for bacterial 16S rRNA genes, we amplified and then pyrosequenced faecal DNA from stool samples. With use of Dirichlet multinomial analysis and mixed models to account for repeated measures, we identified host factors, including development of necrotising enterocolitis, associated with gut bacterial populations. Findings We studied 2492 stool samples from 122 infants in the primary cohort, of whom 28 developed necrotising enterocolitis; 94 infants were used as controls. The microbial community structure in case stools differed significantly from those in control stools. These differences emerged only after the first month of age. In mixed models, the time-by-necrotising-enterocolitis interaction was positively associated with Gammaproteobacteria (p=0·0010) and negatively associated with strictly anaerobic bacteria, especially Negativicutes (p=0·0019). We studied 1094 stool samples from 44 infants in the secondary cohorts. 18 infants developed necrotising enterocolitis (cases) and 26 were controls. After combining data from all cohorts (166 infants, 3586 stools, 46 cases of necrotising enterocolitis), there were increased proportions of Gammaproteobacteria (p=0·0011) and lower proportions of both Negativicutes (p=0·0013) and the combined Clostridia-Negativicutes class (p=0·0051) in infants who went on to develop necrotising enterocolitis compared with controls. These associations were strongest in both the primary cohort and the overall cohort for infants born at less than 27 weeks' gestation. Interpretation A relative abundance of Gammaproteobacteria (ie, Gram-negative facultative bacilli) and relative paucity of strict anaerobic bacteria (especially Negativicutes) precede necrotising enterocolitis in very low birthweight infants. These data offer candidate targets for interventions to prevent necrotising enterocolitis, at least among infants born at less than 27 weeks' gestation. Funding National Institutes of Health (NIH), Foundation for the NIH, the Children's Discovery Institute.