[Abridged] Star and planet formation are the complex outcomes of gravitational collapse and angular momentum transport mediated by protostellar and protoplanetary disks. In this review we focus on the role of gravitational instability in this process. We begin with a brief overview of the observational evidence for massive disks that might be subject to gravitational instability, and then highlight the diverse ways in which the instability manifests itself in protostellar and protoplanetary disks: the generation of spiral arms, small scale turbulence-like density fluctuations, and fragmentation of the disk itself. We present the analytic theory that describes the linear growth phase of the instability, supplemented with a survey of numerical simulations that aim to capture the non-linear evolution. We emphasize the role of thermodynamics and large scale infall in controlling the outcome of the instability. Despite apparent controversies in the literature, we show a remarkable level of agreement between analytic predictions and numerical results. We highlight open questions related to (1) the development of a turbulent cascade in thin disks, and (2) the role of mode-mode coupling in setting the maximum angular momentum transport rate in thick disks.