Textbooks treat black body radiation as radiation in thermal equilibrium with its environment (more specifically - with a black body): Planck's formula is essentially derived from the partition function. \begin{align} \hat{\rho}&=Z^{-1}e^{-\beta H_{ph}},& Z&=tr[e^{-\beta H_{ph}}]& H_{ph}&=\sum_{\mathbf{k},\lambda}\hbar\omega_{\mathbf{k},\lambda} a_{\mathbf{k},\lambda}^\dagger a_{\mathbf{k},\lambda} \end{align} This is usually described as a cavity with a small opening, so that enough radiation escapes through the opening to be observable, but too little to significantly distort the thermal equilibrium.
In practice, this is rarely the case - most radiation described as black body radiation is in a strongly non-equilibrium state, being a flux flowing from a hot object (such as a lamp filament or a star) to much colder surroundings (as discussed in the comments to this question and the answers). Yet, such sources are rather well described by Planck's formula.
I see several possible explanations to this:
- the light source itself serves as a cavity, with the light making many trips back and forth before escaping (this does not sound very plausible to me)
- the light source is sufficiently thick that photons travelling through it are absorbed and reemitted multiple times before exiting the source, thus being in thermal quasi-equilibrium
- the radiation processes in the source are such that they approximate the Planck's curve: e.g., the thermal electrons in a lamp filament are themselves distributed according to Boltzmann's law; whereas in the case of a star, we have a complex composition of chemical elements, whose spectral lines form a nearly continuous spectrum, and whose excited states are occupied according to the Boltzmann law. While this sounds plausible, it significantly deviates from the reasoning that leads to Planck's formula.
Remarks:
- Another inconsistency in the Planck's formula, that is worth noting is that cavity radiation has discrete spectrum.
- A similar question, although less detailed, Relevant question on black-body aspects of solar radiation
- Another fine point that has surfaced in the answers here and those to a related question is whether black body radiation is radiation in thermal equilibrium OR whether it is the radiation emitted by the black body. In the former case, the bodies in which the radiation is in contact do not necessarily have to be black for the radiation to be described by the Planck's formula. In the latter case, the radiation does not have to be in equilibrium (although it would mean that neither is the black body, since it loses the energy via radiating). One could derive Planck's formula in both cases, which correspond to the second and third options proposed above (radiation comes to thermal equilibrium vs. the filament/star emits radiation that is already black.)
- Thread with discussion of how/whether/when discrete atomic spectrum in a gas gives rise to black body radiation: Difference in thermal radiation between condensed matter and gases
- Another related thread: What are the various physical mechanisms for energy transfer to the photon during blackbody emission?