This is a fairly difficult result. You can find a proof in, say, Qing Liu's Algebraic Geometry and Arithmetic Curves (Proposition 2.5.23 on page 74 of the 2006 edition). Among other things, the proof relies upon the characterisation of dimension of varieties using transcendence degree, which in turn relies upon on nontrivial theorems from commutative algebra such as Krull's Hauptidealsatz.
As proven in Liu, if $k$ is a field and $A$ is a finitely generated algebra over $k$ which is an integral domain, then for every prime ideal $\mathfrak p\subset A$, we have $\operatorname{ht}(\mathfrak p)+\dim(A/\mathfrak p)=\dim(A)$. By taking $\mathfrak p\subset A$ to be a maximal ideal in this theorem, we see that $\dim(A_\mathfrak p)=\operatorname{ht}(\mathfrak p)=\dim(A)$.
Let $X$ be an irreducible, but not necesesarily reduced, affine variety over $k$. Geometrically, the first assertion of the preceding paragraph corresponds to the fact that if $Y\subseteq X$ is a closed subvariety, then $\operatorname{codim}(Y,X)+\dim(Y)=\dim(X)$. The second assertion corresponds to the fact that if $x\in X$ is a closed point, then $\dim(X)=\dim(\mathcal O_{X,x})$.
You can find more details in this thread. Note that these results are very much special to varieties (as opposed to more general schemes). Even if $X$ is an irreducible Noetherian affine scheme, it is not necessarily the case that $\operatorname{codim}(Y,X)+\dim(Y)=\dim(X)$ for closed subschemes $Y\subseteq X$. See Chapter 5 of Görtz and Wedhorn's Algebraic Geometry I: Schemes (in particular Remark 5.29 and Exercise 5.7).