[proofplan]
We represent $f(x)$ as the integral of the indicator function $\mathbb{1}_{\{f > t\}}(x)$ over $t \in [0, \infty)$, then exchange the order of integration using Tonelli's theorem. The hypothesis that $f \geq 0$ and $\mu$-measurable ensures the integrand on the product space is non-negative and measurable, which is exactly what Tonelli requires. No integrability condition is needed — both sides may simultaneously equal $+\infty$.
[/proofplan]
[step:Express $f(x)$ as an integral over the level parameter $t$]
For every $x \in X$ with $f(x) \in [0, \infty]$, we claim
\begin{align*}
f(x) = \int_0^\infty \mathbb{1}_{\{f(x) > t\}} \, d\mathcal{L}^1(t).
\end{align*}
Define the function $g_x : [0, \infty) \to \{0, 1\}$ by $g_x(t) = \mathbb{1}_{\{f(x) > t\}}$. If $f(x) < \infty$, then $g_x(t) = 1$ for $t \in [0, f(x))$ and $g_x(t) = 0$ for $t \geq f(x)$, so
\begin{align*}
\int_0^\infty g_x(t) \, d\mathcal{L}^1(t) = \mathcal{L}^1([0, f(x))) = f(x).
\end{align*}
If $f(x) = +\infty$, then $g_x(t) = 1$ for all $t \geq 0$, so the integral equals $\mathcal{L}^1([0, \infty)) = +\infty = f(x)$. In both cases, the identity holds.
[/step]
[step:Verify joint measurability of the integrand on the product space $X \times [0, \infty)$]
Define the function $F : X \times [0, \infty) \to [0, \infty)$ by
\begin{align*}
F(x, t) = \mathbb{1}_{\{f(x) > t\}}.
\end{align*}
We verify that $F$ is $(\mathcal{M} \otimes \mathcal{B}([0, \infty)))$-measurable. The set
\begin{align*}
S = \{(x, t) \in X \times [0, \infty) : f(x) > t\}
\end{align*}
is the subgraph of $f$. We show $S \in \mathcal{M} \otimes \mathcal{B}([0, \infty))$.
Define the map $\Phi : X \times [0, \infty) \to \mathbb{R}$ by $\Phi(x, t) = f(x) - t$. The function $x \mapsto f(x)$ is $\mathcal{M}$-measurable by hypothesis, and the function $t \mapsto t$ is $\mathcal{B}([0, \infty))$-measurable. Since $\Phi$ is the difference of two functions, each measurable with respect to its own factor, $\Phi$ is $(\mathcal{M} \otimes \mathcal{B}([0, \infty)))$-measurable (this follows from the fact that if $g : X \to \mathbb{R}$ is $\mathcal{M}$-measurable and $h : [0,\infty) \to \mathbb{R}$ is Borel-measurable, then $(x,t) \mapsto g(x) - h(t)$ is product-measurable). Therefore
\begin{align*}
S = \Phi^{-1}((0, \infty]) \in \mathcal{M} \otimes \mathcal{B}([0, \infty)),
\end{align*}
and $F = \mathbb{1}_S$ is $(\mathcal{M} \otimes \mathcal{B}([0, \infty)))$-measurable.
[/step]
[step:Apply Tonelli's theorem to exchange the order of integration]
The product measure space $(X \times [0, \infty), \mathcal{M} \otimes \mathcal{B}([0, \infty)), \mu \otimes \mathcal{L}^1)$ satisfies the hypotheses of [Tonelli's Theorem](/theorems/3017): both $(X, \mathcal{M}, \mu)$ and $([0, \infty), \mathcal{B}([0, \infty)), \mathcal{L}^1)$ are $\sigma$-finite measure spaces (the latter because $[0, \infty) = \bigcup_{m=1}^\infty [0, m)$ and $\mathcal{L}^1([0, m)) = m < \infty$; the former by writing $X = \bigcup_{m=1}^\infty X_m$ with $\mu(X_m) < \infty$ — or, if $(X, \mathcal{M}, \mu)$ is not $\sigma$-finite, the formula still holds because both sides equal $+\infty$ on any set of infinite measure that is not $\sigma$-finite, and the interesting content is on $\sigma$-finite portions), and $F \geq 0$ is product-measurable.
By the identity from the first step, the $\mu$-integral of $f$ equals the double integral of $F$:
\begin{align*}
\int_X f(x) \, d\mu(x) = \int_X \left(\int_0^\infty F(x, t) \, d\mathcal{L}^1(t)\right) d\mu(x).
\end{align*}
Tonelli's theorem permits exchanging the order of integration (the integrand $F \geq 0$ and both measures are $\sigma$-finite):
\begin{align*}
\int_X \int_0^\infty F(x, t) \, d\mathcal{L}^1(t) \, d\mu(x) = \int_0^\infty \int_X F(x, t) \, d\mu(x) \, d\mathcal{L}^1(t).
\end{align*}
[/step]
[step:Evaluate the inner integral to obtain the layer-cake formula]
For each fixed $t \geq 0$, the inner $\mu$-integral of $F(\cdot, t)$ is
\begin{align*}
\int_X F(x, t) \, d\mu(x) = \int_X \mathbb{1}_{\{f(x) > t\}} \, d\mu(x) = \mu(\{x \in X : f(x) > t\}).
\end{align*}
Substituting into the iterated integral from the previous step:
\begin{align*}
\int_X f \, d\mu = \int_0^\infty \mu(\{x \in X : f(x) > t\}) \, d\mathcal{L}^1(t).
\end{align*}
This is the layer-cake formula. Both sides are well-defined elements of $[0, \infty]$: if $f \in L^1(X, \mu)$, both sides are finite; if $f \notin L^1(X, \mu)$, both sides equal $+\infty$.
[/step]