Your question is eminently sensible and indeed important.
The keyword here is Mean Value Theorem, or more specifically its special case that a function defined and differentiable on an interval with identically zero derivative must be constant. For more details, see (e.g.) $\S 5.1$ of these notes.
Here are some further remarks:
$\bullet$ Of course it is understood here that all functions are defined on an interval $I$. If we look at functions with more complicated -- and in particular disconnected -- domains, then uniqueness of primitives up to a constant need not hold. One sees the ugly head of this in freshman calculus when students are taught (not by me!) that the antiderivatives of $\frac{1}{x}$ are $\log |x| +C$.
$\bullet$ The fact that a function which is differentiable on an interval with identically zero derivative is constant -- I call this the Zero Velocity Theorem -- is actually quite deep. As mentioned above, it is a consequence of the Mean Value Theorem. In turn it holds in an ordered field iff the field is Dedekind complete, as is shown in Jim Propp's nice article Real Analysis in Reverse. It is easy to construct counterexamples over $\mathbb{Q}$: just take a piecewise constant function with discontinuities at irrational numbers.
$\bullet$ A proof of the Zero Velocity Theorem directly from an equivalent of the least upper bound axiom -- specifically, using Real Induction -- is given here.