The Green-Kubo formula relates the spatial diffusion coefficient to the stationary velocity autocorrelation function. We derive a generalization of the Green-Kubo formula that is valid for systems with long-range or nonstationary correlations for which the standard approach is no longer valid. For the systems under consideration, the velocity autocorrelation function ⟨v(t+τ)v(t)⟩ asymptotically exhibits a certain scaling behavior and the diffusion is anomalous, ⟨x2(t)⟩≃2Dνtν. We show how both the anomalous diffusion coefficient Dν and the exponent ν can be extracted from this scaling form. Our scaling Green-Kubo relation thus extends an important relation between transport properties and correlation functions to generic systems with scale-invariant dynamics. This includes stationary systems with slowly decaying power-law correlations, as well as aging systems, systems whose properties depend on the age of the system. Even for systems that are stationary in the long-time limit, we find that the long-time diffusive behavior can strongly depend on the initial preparation of the system. In these cases, the diffusivity Dν is not unique, and we determine its values, respectively, for a stationary or nonstationary initial state. We discuss three applications of the scaling Green-Kubo relation: free diffusion with nonlinear friction corresponding to cold atoms diffusing in optical lattices, the fractional Langevin equation with external noise recently suggested to model active transport in cells, and the Lévy walk with numerous applications, in particular, blinking quantum dots. These examples underline the wide applicability of our approach, which is able to treat very different mechanisms of anomalous diffusion.