In Classical Mechanics the configuration of a system can be characterized by some point $s\in \mathbb{R}^n$ for some $n$. In particular, if it's a system of $k$ particles then $n = 3k$ and if there are holonomic constraints then in truth $s$ lies in some submanifold of $\mathbb{R}^n$. Even if the constraints are not holonomic, the configuration of a system can still be given by elements of some finite dimensional smooth manifold.
In that case, the Lagrangian becomes a smooth function $L: TM\to \mathbb{R}$ where $TM$ is the tangent bundle of the configuration manifold. Given coordinates $(q^1,\dots,q^n)$ on $M$ we can therefore make coordinates $(q^1,\dots,q^n,\dot{q}^1,\dots,\dot{q}^n)$ on $TM$ such that $q^i$ on $TM$ is really $q^i\circ \pi$ and $\dot{q}^i$ is characterized by the fact that if $v \in T_aM$ is
$$v = \sum_{i=1}^n v^i\dfrac{\partial}{\partial q^i}\bigg|_a$$
Then $\dot{q}^i(v) = v^i$. In that way, differentiating with respect to $q^i$ and $\dot{q}^i$ is perfectly well defined and Lagrange's Equation is totally meaningfull
$$\dfrac{d}{dt} \dfrac{\partial L}{\partial \dot{q}^i}(c(t),c'(t)) = \dfrac{\partial L}{\partial q^i}(c(t),c'(t))$$
When it comes then to studying fields like electromagnetic fields and so on, things get a little messy. Now, the system is the field and a configuration of the field is not anymore a certain list of numbers but a function like $\mathbf{E}: \mathbb{R}^3\to T\mathbb{R}^3$ or $\phi : \mathbb{R}^3\to \mathbb{R}$.
If we insist in building a configuration space $M$ it will be infinite dimensional and locally modeled on Banach Spaces. If we try to mimic Lagrangian formalism here, it'll end up in some infinite dimensional bundle, and this is not something nice to work with.
Now, most books work formally. For example, they let $\mathcal{L} = \dfrac{1}{2}g_{\mu\nu}(\partial^\nu \phi)(\partial^\mu\phi)- \dfrac{1}{2}m^2\phi^2$. Then they compute formally:
$$\dfrac{\partial \mathcal{L}}{\partial \phi} = -m^2\phi \\ \dfrac{\partial \mathcal{L}}{\partial (\partial_\mu \phi)} = \partial^\mu \phi$$
And then Lagrange's Equations becomes
$$\dfrac{\partial \mathcal{L}}{\partial \phi} = \partial_\mu \dfrac{\partial \mathcal{L}}{\partial (\partial_\mu\phi)}\Longrightarrow \partial_\mu\partial^\mu\phi + m^2\phi = 0$$
Now this brings some questions:
First of them it is not clear on which space this $\mathcal{L}$ is defined and where it takes values. Some people say it is just a $3$-form on spacetime, but it doesn't seem like that, it looks like a scalar to me.
Second, we take derivatives of $\mathcal{L}$ with respect to functions. This is much confusing to me. It even conflicts the first point of view, if $\mathcal{L}$ is a $3$-form it can only be differentiated with respect to the coordinates of the manifold on which it is defined.
So how can we make all of this rigorous? I mean, in which space is $\mathcal{L}$ defined? What these derivatives really mean and why they make any sense at all? How to make a connection between this and the Classical Mechanics Lagrangian formalism?