We define degeneracy loci for vector bundles with structure group G_2, and give formulas for their cohomology (or Chow) classes in terms of the Chern classes of the bundles involved.When the base is a point, such formulas are part of the theory for projective homogeneous spaces developed by Bernstein--Gelfand--Gelfand and Demazure.This has been extended to the setting of general algebraic geometry by Giambelli--Thom--Porteous, Kempf--Laksov, and Fulton in classical types; the present work carries out the analogous program in type G_2.We include explicit descriptions of the G_2 flag variety and its Schubert varieties, and several computations, including one that answers a question of William Graham.As part of our description of the G_2 flag variety, we prove some basic facts about octonions and trilinear forms, and give a natural construction of octonion algebra bundles which appears to be new.Motivated by the relationship between symmetric matrices and the symplectic group, we define a new type of symmetry for morphisms of vector bundles, called triality symmetry.We explain the relation with G_2, and deduce degeneracy locus formulas for triality-symmetric morphisms from formulas for Schubert loci in G_2 flag bundles.We also give a proof of the formulas in terms of equivariant cohomology, by computing the classes of P-orbits in g_2/p for a parabolic subgroup P in G_2.In five appendices, we collect some facts from representation theory; review the phenomenon of triality and its relation to G_2 flags; discuss a general notion of symmetry for morphisms of vector bundles; give parametrizations of Schubert cells, formulas for degeneracy loci, and the equivariant multiplication table for the G_2 flag variety; and compute the Chow rings of quadric bundles.