The definition of a weak derivative comes from a more general setup called the theory of distributions also called theory of generalized functions formalized by the French mathematician Laurent Schwartz in the 1940's.
Let $\Omega$ be an open subset of $\mathbb R^n$. We define the space of distributions $\mathcal D'(\Omega)$ as being the topological dual of the space of test functions $C^\infty_c(\Omega)$. For more details about this topology you can refer to Walter Rudin's Functional Analysis (pp. 151-153).
For a given distribution $\tau$, its weak i-th derivative $\partial_{x_{i}}\tau$ in the sense of distributions is defined as $$\begin{equation} \forall \phi \in C^\infty_c(\Omega) \;\;\langle\partial_{x_{i}}\tau, \phi\rangle \;:= -\langle\tau, \partial_{x_{i}}\phi\rangle \end{equation}$$
To come back to your question, there is a canonical injection of the space $L^1_{loc}(\Omega)\hookrightarrow \mathcal D'(\Omega)$, indeed one can show that $$\langle f,\phi\rangle \;= \int_{\Omega} f\phi$$ for $f \in L^1_{loc}(\Omega)$ is a distribution. Then we define a weak derivative for an $L^1_{loc}(\Omega)$ function as its derivative in the sense of distributions:
$$\forall \phi \in C^\infty_c(\Omega)\;\; \langle f', \phi\rangle \;:= - \langle f, \phi'\rangle$$
Which is also: $$\int_{\Omega} f'\phi = - \int_{\Omega}f\phi'$$
Hope this helps!
Edit: found this great topic: Are weak derivatives and distributional derivatives different?