I would like to add that in some cases, even if a term is a total derivative -- so a surface term, by Stokes's theorem -- we can't neglect it in QFT! This will also expand a little on Valter Moretti's point that we need to use some sort of boundary conditions.
For this purpose, let us consider $$S_\theta = \int_M \operatorname{tr} F \wedge F.$$
Here $M$ is 4-dimensional spacetime, $F$ is the gauge field strength for a non-Abelian gauge theory, $F = dA + A \wedge A$, so its components are matrices, that's why we have the trace. You may be unfamiliar with this $\wedge$ and $d$. They're called the "wedge product" and "exterior derivative". In index notation, they correspond to taking the anti-symmetric product; and to taking the derivative, then the totally anti-symmetric part, so: $A \wedge A $ corresponds to $A_\mu A_\nu - A_\nu A_\mu$ and $dA$ to $\partial_\mu A_\nu - \partial_\nu A_\nu$. Note that $A \wedge A \neq 0$ because each component of $A$ is a matrix. You can do all of this with Levi-Civitas, e.g., $$S_\theta = \int d^4 x \, \operatorname{tr} \epsilon_{\mu\nu\rho\sigma} F^{\mu\nu} F^{\rho\sigma} \tag{1} $$
but that leads to writing more indices than I want to.
Anyway, it turns out that $$\operatorname{tr} F\wedge F = d \operatorname{tr} (dA\wedge A + \frac{2}{3} A\wedge A \wedge A)$$
(to show this: $d^2 = 0$, the product rule applies to $d$, and $\operatorname{tr} A^{\wedge 4} = 0$ because of the cyclic property of the trace.) So we have that by Stokes's theorem $$S_\theta = \int_{S^3} \operatorname {tr} (dA\wedge A + \frac{2}{3} A \wedge A \wedge A) $$
where $S^3$ is a 3-sphere at infinity, the "boundary of spacetime". As boundary condition, we should use that $F = 0$ at infinity. Then $$S_\theta = -\frac{1}{3} \int_{S^3} \operatorname A\wedge A \wedge A.$$
Now, if $F = 0$, it is possible to find a gauge transformation such that $A = 0$. So is $S_\theta = 0$? No!
But how? It's topological. $A$ defines a function $S^3 \to G$ where $G$ is the gauge group. Such a function belongs to $\pi_3$, which stands for something called the "third homotopy group", which is a generalization of the concept of winding number. You know how if draw a closed curve -- topologically a circle -- in the plane without crossing through the origin, the number of times it winds around the origin cannot be changed by bending stretching the curve smoothly? The third homotopy group is like that, except it's about how 3-spheres wrap around the target space. (I don't know how to visualize it either!)
If $G = SU(n)$ with $n\ge 2$, then $\pi_3$ is just like the winding number: it's an integer. What this means is that unless the "winding number" of $A$ is $0$, a gauge transformation can't make $A = 0$ everywhere on $S^3$.
So we have shown that $S_\theta \neq 0$ in general, even when we use the boundary condition $F =0$ at infinity. In fact $S_\theta = \theta n $ where $n$ is the "winding number" and $\theta$ is some constant. But why should we care? Because it is a surface term, and variations are taking with the boundary condition that the variation vanish on the boundary, $S_\theta$ makes no contribution to the Euler-Lagrange field equations. But this is quantum field theory, and I really want the path integral $$\begin{align} \mathcal Z &= \int D[A, \psi, \ldots]\, \exp(-i(S_0[A, \psi, \ldots] + S_\theta)) \\
& = \sum_n \int D[A, \psi, \ldots]\, \exp(-i S_0[A, \psi, \ldots]) e^{-in\theta} \end{align} $$
where on the second line, each integral is only over $A$ such that the winding number is $n$. So $S_\theta$ determines interference terms between different paths. This means it can -- and does -- have physical effects in quantum field theory. For example, from (1) you can see that because there is a Levi-Civita, $S_\theta$ violates parity symmetry.
I think these topological arguments with winding numbers and other similar concepts are really cool. So does the Nobel committee, because this year's prize was about things like this. If you want to learn more, some keywords to look for are instantons, topological quantum field theory (TQFT), theta terms, strong $CP$ problem, axions...