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. In the next part of our review, we focus on the astrophysical consequences of the instability. We show that the disks most likely to be gravitationally unstable are young and relatively massive compared to their host star, M d /M * ≥ 0.1. They will develop quasi-stable spiral arms that process infall from the background cloud. While instability is less likely at later times, once infall becomes less important, the manifestations of the instability are more varied. In this regime, the disk thermodynamics, often regulated by stellar irradiation, dictates the development and evolution of the instability. In some cases the instability may lead to fragmentation into bound companions. These companions are more likely to be brown dwarfs or stars than planetary mass objects. Finally, 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. We dedicate this article to the memory of Francesco Palla, who has been a source of inspiration for star formation and disk studies throughout the years. Through his support and encouragement of young scientists, his contributions live on.