We study definably amenable NIP groups. We develop a theory of generics showing that various definitions considered previously coincide, and we study invariant measures. As applications, we characterize ergodic measures, give a proof of the conjecture of Petrykowski connecting existence of bounded orbits with definable amenability in the NIP case, and prove the Ellis group conjecture of Newelski and Pillay connecting the model-theoretic connected component of an NIP group with the ideal subgroup of its Ellis enveloping semigroup.