Fix a measurable space $(X,\B)$. A measure on $(X,\B)$ allows us to assign sizes to subsets of $X$. In this section we are going to understand how measures give rise to integrals. The key is the we can associate to each measurable set a function on $X$ whose integral can reasonably be thought of as the measure of $X$. We will then integrate more complicated functions by approximations.
Fix a set $X$ and $A \subset X$. The indicator function of $A$ is the function \[ 1_A(x) = \begin{cases} 1 & x \in A \\ 0 & x \notin A \end{cases} \] from $X$ to $\R$.
Fix a measurable space $(X,\B)$. For every $B \in \mathscr{B}$ the function $1_B : X \to \R$ is $(\B,\P(\R))$ measurable.
Fix $E \subset \R$. The set $(1_B)^{-1}(E)$ can only be $\emptyset$, $B$, $X \setminus B$ or $X$ according to which of $0,1$ belong to $E$. All four sets belong to $\mathscr{B}$ because $B \in \mathscr{B}$. Therefore $1_B$ is $(\B,\P(\R))$ measurable.
Fix a measurable space $(X,\B)$. A function $f : X \to \R$ is simple if \[ f = a_1 1_{B(1)} + \cdots + a_k 1_{B(k)} \] for sets $B(1),\dots,B(k)$ in $\B$ and numbers $a_1,\dots,a_k$.
The expression \[ a_1 1_{B(1)} + \cdots + a_k 1_{B(k)} \] is not unique. Indeed, when $B(1)$ and $B(2)$ are disjoint we can write \[ 1_{B(1)} + 1_{B(2)} = 1_{B(1) \cup B(2)} \] and when $B(1) \cap B(2)$ is non-empty we can write \[ a_1 1_{B(1)} + a_2 1_{B(2)} = a_1 1_{B(1) \setminus B(2)} + (a_1 + a_2) 1_{B(1) \cap B(2)} + a_2 1_{B(2) \setminus B(1)} \] however, it is always possible to write a simple function in the form \[ a_1 1_{B(1)} + \cdots + a_k 1_{B(k)} \] where the sets $B(1),\dots,B(k)$ are disjoint.
A function $f : X \to \R$ is simple if and only if its range is finite.
If $f$ can be written as \[ a_1 1_{B(1)} + a_2 1_{B(2)} + \cdots + a_k 1_{B(k)} \] then its range is finite, as $f$ can only take values obtained from adding together at most $k$ of the terms $a_1,\dots,a_k$. On the other hand, if the only values $f$ takes are $c_1,\dots,c_k$ then \[ f = c_1 1_{f^{-1}(\{c_1\})} + \cdots + c_k 1_{f^{-1}(\{c_k\})} \] expresses $f$ as a simple function.
Fix a measure space $(X,\B,\mu)$. We define the integral of a simple, measurable function $f : X \to \R$ with respect to $\mu$ to be \[ \int f \intd \mu = \sum_{t \in \R} t \cdot \mu(f^{-1}(\{t\})) = \sum_{i=1}^n a_i \cdot \mu(f^{-1}(\{a_i\})) \] where $a_1,\dots,a_n$ are the values that $f$ takes.
We will first check that the integral of simple functions is linear.
Let $(X,\B,\mu)$ be a measure space. Let $f,g : X \to \R$ be simple and measurable. Then \[ \begin{gathered} \int f + g \intd \mu = \int f \intd \mu + \int g \intd \mu \\ \alpha \int f \intd \mu = \int \alpha f \intd \mu \end{gathered} \] for all $\alpha \in \R$.
The second property is immediate. For the first, write \[ f = \sum_{i=1}^m a_i 1_{A(i)} \qquad g = \sum_{j=1}^r b_j 1_{B(j)} \] with $A(1),\dots,A(m)$ pairwise disjoint and $B(1),\dots,B(r)$ also pairwise disjoint. We can write \[ A(i) = \bigcup_{j=1}^r A(i) \cap B(j) \qquad B(j) = \bigcup_{i=1}^m B(j) \cap A(i) \] with both unions consisting of pairwise disjoint collections. Thus \[ \begin{gathered} f(x) = \sum_{i=1}^m \sum_{j=1}^r a_i 1_{A(i) \cap B(j)} \\ g(x) = \sum_{j=1}^r \sum_{i=1}^m b_i 1_{B(j) \cap A(i)} \\ (f+g)(x) = \sum_{i=1}^m \sum_{j=1}^r (a_i + b_j) 1_{A(i) \cap B(j)} \end{gathered} \] and adding the integrals of the first two simple functions gives the integral of the third simple function. ▮
Now that we know how to integrate simple functions, we move on to non-negative measurable functions.
Fix a measure space $(X,\B,\mu)$ and a measurable function $f : X \to [0,\infty]$. Define the integral of $f$ with respect to $\mu$ to be \[ \int f \intd \mu = \sup \left\{ \int \phi \intd \mu : \phi \text{ a simple function with } 0 \le \phi \le f \right\} \] the idea being to approximate the area under the graph of $f$ by the integral of a simple function.
Fix $f,g : X \to [0,\infty]$ measurable.
The first of these is immediate because every simple function $0 \le \phi \le f$ satisfies $0 \le \phi \le g$. For the second, if $\alpha = 0$ both sides are zero, and if $\alpha > 0$ then the result follows because it is true for simple functions. ▮
We next want to prove that the integral is linear. We will not do this directly, instead proving first the fundamental convergence result on integration with respect to a measure.
For any measure space $(X,\B,\mu)$ and any non-decreasing sequence $f_n : X \to [0,\infty]$ of measurable functions the equality \[ \lim_{n \to \infty} \int f_n \intd \mu = \int \lim_{n \to \infty} f_n \intd \mu \] holds.
Define $g$ to be the pointwise limit of the sequence $n \mapsto f_n$ of functions. It is well-defined and measurable as a function from $X$ to $[0,\infty]$. We have \[ \int f_n \intd \mu \le \int g \intd \mu \] for all $n \in \N$ by monotonicity. Therefore \[ \lim_{n \to \infty} \int f_n \intd \mu \le \int \lim_{r \to \infty} f_r \intd \mu \] holds. It remains to prove the reverse inequality.
For the reverse inequality fix $0 \l \alpha \l 1$ and fix $0 \le \phi \le g$ a simple function. Put \[ E(n) = \{ x \in X : f_n(x) \ge \alpha \phi(x) \} \] and note that $\phi \le f$ together with $0 \l \alpha \l 1$ implies the sequence $n \mapsto E_n$ is increasing and has union $X$. Writing $\{ a_1,\dots,a_r \}$ for the range if $\phi$ we can then calculate that \[ \begin{aligned} \int 1_{E(n)} \phi \intd \mu &= \sum_{i=1}^r a_r \mu( \phi^{-1}(a_r) \cap E(n)) \\ &\to \sum_{i=1}^r a_r \mu(\phi^{-1}(a_r)) = \int \phi \intd \mu \end{aligned} \] where the convergence follows from continuity of measures. But then \[ \lim_{n \to \infty} \int f_n \intd \mu \ge \lim_{n \to \infty} \int 1_{E(n)} f_n \intd \mu \ge \lim_{n \to \infty} \int 1_{E(n)} \alpha \phi \intd \mu = \alpha \int \phi \intd \mu \] which finishes the proof because $0 \l \alpha \l 1$ was arbitrary and $0 \le \phi \le f$ was arbitrary. ▮
As a consequence of the monotone convergence theorem we get linearity and a useful convergence result.
Whenever $f,g : X \to [0,\infty]$ are measurable \[ \int f+g \intd \mu = \int f \intd \mu + \int g \intd \mu \] holds.
Fix sequences $n \mapsto \phi_n$ and $n \mapsto \psi_n$ of simple functions that take non-negative values and satisfy $\phi_n \to f$ and $\psi_n \to g$. Then $n \mapsto \phi_n + \psi_n$ is a sequence of non-negative simple functions that converges to $f + g$. We have already checked that \[ \int \phi_n + \psi_n \intd \mu = \int \phi_n \intd \mu + \int \psi_n \intd \mu \] and the monotone convergence theorem allows us to take the limits on both sides by evaluating the limits inside the integrals, giving the desired conclusion. ▮