A key question in population biology is understanding the conditions under which the species of an ecosystem persist or go extinct. Theoretical and empirical studies have shown that persistence can be facilitated or negated by both biotic interactions and environmental fluctuations. We study the dynamics of n interacting species that live in a stochastic environment. Our models are described by n dimensional piecewise deterministic Markov processes. These are processes (X(t), r(t)) where the vector X denotes the density of the n species and r(t) is a finite state space process which keeps track of the environment. In any fixed environment the process follows the flow given by a system of ordinary differential equations. The randomness comes from the changes or switches in the environment, which happen at random times. We give sharp conditions under which the populations persist as well as conditions under which some populations go extinct exponentially fast. As an example we look at the competitive exclusion principle from ecology, which says in its simplest form that two species competing for one resource cannot coexist, and show how the random switching can facilitate coexistence.