Environmental change, if severe, can drive a population extinct unless the population succeeds in adapting to the new conditions. How likely is a population to win the race between population decline and adaptive evolution? Assuming that environmental degradation progresses across a habitat, we analyze the impact of several ecological factors on the probability of evolutionary rescue. Specifically, we study the influence of population structure and density-dependent competition as well as the speed and severity of environmental change. We also determine the relative contribution of standing genetic variation and new mutations to evolutionary rescue. To describe population structure, we use a generalized island model, where islands are affected by environmental deterioration one after the other. Our analysis is based on the mathematical theory of time-inhomogeneous branching processes and complemented by computer simulations. We find that in the interplay of various, partially antagonistic effects, the probability of evolutionary rescue can show nontrivial and unexpected dependence on ecological characteristics. In particular, we generally observe a nonmonotonic dependence on the migration rate between islands. Counterintuitively, under some circumstances, evolutionary rescue can occur more readily in the face of harsher environmental shifts, because of the reduced competition experienced by mutant individuals. Similarly, rescue sometimes occurs more readily when the entire habitat degrades rapidly, rather than progressively over time, particularly when migration is high and competition strong.