I can understand that $GL(n,\mathbb{R})\cong \mathbb{R^{n^2}}$ and thus it has dimension $n^2$. But why is the dimension of $SL(n,\mathbb{R})$ $n^2-1$?
More generally, how do you get the dimension of matrix groups like $O(n),SO(n),U(n),SU(n)$?
I've learned that the dimensions of a Lie group and its Lie algebra are the same. is this fact used to show that?
Then, how does one find the Lie algebras of these groups?
There are two separate questions here: How to compute the dimensions of various matrix Lie groups, and how to compute their Lie algebras. I'll address the first.
By definition, $SL(n, \Bbb R)$ is the kernel of the determinant $\det : GL(n, \Bbb R) \to \Bbb R^*$, which has constant rank $1$. Thus (by what, e.g., Lee's Introduction to Smooth Manifolds, $\S$8, calls the Regular Level Set Theorem) $$\dim SL(n, \Bbb R) = \dim GL(n, \Bbb R) - \textrm{rank} \det = n^2 - 1 .$$
The other groups can be handled similarly for other, if slightly more sophisticated choices of map. For example, $O(n, \Bbb R)$ is the kernel of map $\Phi : GL(n, \Bbb R) \to GL(n, \Bbb R)$ defined by $\Phi : A \mapsto A^{\top} A$. In fact, since $\Phi(A)^{\top} = \Phi(A)$, we could regard $\Phi$ as a map into the space $S(n, \Bbb R)$ of symmetric $n \times n$ matrices, in which case it is a submersion (which you should check!), so that $$\dim O(n, \Bbb R) = \dim GL(n, \Bbb R) - \dim S(n, \Bbb R) = n^2 - \tfrac{1}{2} n (n + 1) = \tfrac{1}{2} n (n - 1).$$
Since $SO(n, \Bbb R)$ is an open subgroup of $O(n, \Bbb R)$, $\dim SO(n, \Bbb R) = \dim O(n, \Bbb R)$.