Sigma algebra definition and Lebesgue integration

I know the definition of the $\sigma$-algebra, and I have seen it used in integration theory. However, I do not understand why it is defined the way it is.

From what I understand, the definition arose from integration theory, but why is the Lebesgue integral defined on a $\sigma$-algebra?

There are detailed answers to this question through the cases where Riemann integral fails, but I could never understand those clearly. I could try to answer your question this way: Lebesgue integral (of a complex-valued function) is defined through the integral of a positive function. The latter one, in turn is defined as a limit of an integral of a simple function. I think it is here that you need to use the countable additivity of the sigma-algebra and of the measure: as you recall, simple positive function has a finite number of values. But when you go to the limit to get an integral of a positive function ($\lim \sum_{i=1}^n s_i \mu(A_i)$), you would need a nice property for $\mu$, because $n$ might go to the infinity.
The issue, as I see it, is this: if one postulates the existence of a translation invariant measure on $\mathbb R$, one can show that such a measure cannot exist. Now, the intuitive notion of length in $\mathbb R$ can be used to define something akin to a measure, by approximating the size of a set with the sum of the sizes of intervals that contain it: this is what we call "Lebesgue's outer measure".
But, as mentioned above, one can show that such outer measure cannot be a measure over every subset of $\mathbb R$. So Caratheodory's idea (I'm not completely sure if I'm assigning priority correctly here) was to consider a certain family of subsets of $\mathbb R$ where he was able to show that Lesbesgue's outer measure is actually a measure; the properties of $\sigma$-algebra play a key role in his proof. This family is what we call the $\sigma$-algebra of Lebesgue-measurable sets. I guess eventually it was realized that the notion of $\sigma$-algebra was the right environment where to define a measure.