Dimension of $SL(n,\mathbb{R})$ and some other Lie groups 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?
 A: 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)$.

 For $U(n)$, take $\Psi : GL(n, \Bbb R) \to GL(n, \Bbb R)$ to be $A \mapsto A^* A$; as in the orthogonal case, we may replace the codomain with the space $H(n, \Bbb R)$ of Hermitian matrices, and this anyway leads to $\dim U(n) = n^2$. For $SU(n)$, consider the map $\det_{\Bbb C} : U(n) \to \Bbb S^1$, which leads to $\dim SU(n) = n^2 - 1$.

A: I'll focus on the question of finding the Lie algebra of a matrix group.  As it turns out, Lie algebras of matrix groups can be described in a particular way:

For a matrix Lie group $G$, the lie algebra $\mathfrak g$ can be described as 
  $$
\mathfrak g = \{X: \exp(tX) \in G \quad \text{for all } t \in \Bbb R\}
$$
  where $\exp$ denotes the matrix exponential.

and it can be shown that this coincides with the tangent space of $G$ at the identity matrix.
For our case, note that we're looking for the set of $X$ satisfying $\det(\exp(tX)) = 1$ for all real $t$.  Note that
$$
\det(\exp(tX)) = e^{\operatorname{Trace}(tX)} = e^{t\operatorname{Trace}(X)}
$$
The above is equal to $1$ for all $t$ if and only if $\operatorname{Trace}(X) = 0$.
Thus, it suffices to determine the dimension of the space 
$$
\mathfrak{sl} = \{X : \operatorname{Trace}(X) = 0\}
$$
Since the trace is a non-zero functional on an $n^2$-dimensional space, the rank-nullity theorem lets us deduce that the dimension of this Lie algebra is $n^2-1$, as desired.
The Lie algebras of the other groups you mention can be derived similarly.
A: By definition, one has:
$$\textrm{SL}_n(\mathbb{R})={\det}^{-1}(\{1\}).$$
Now, it is easily shown that $\det\colon\mathcal{M}_n(\mathbb{R})\rightarrow\mathbb{R}$ is a submersion at any point of $\mathrm{SL}_n(\mathbb{R})$, so that $\mathrm{SL}_n(\mathbb{R})$ is a submanifold of codimension $1$ in $\mathcal{M}_n(\mathbb{R})$. Whence the result.
Indeed, since for all $X\in\textrm{SL}_n(\mathbb{R})$, $M\mapsto XM$ is a smooth diffeomorphism of $\mathcal{M}_n(\mathbb{R})$, it suffices to prove that $\det$ is a submersion at $I_n$, but notice that:
$$\mathrm{d}_{I_n}\det\cdot H=\textrm{tr}(H)\tag{1}.$$
Which is a non-zero linear form and hence surjective. Furthermore, one has:
$$\mathfrak{sl}_n(\mathbb{R}):=T_{I_n}\textrm{SL}_n(\mathbb{R})=\ker(\mathrm{d}_{I_n}\det)=\{H\in\mathcal{M}_n(\mathbb{R})\textrm{ s.t. }\textrm{tr}(H)=0\}\tag{2}.$$
If you are wondering about why $(1)$ and $(2)$ hold, I recommend you check this answer.
In a similar fashion, you can show that:
$\textrm{O}(n)$ has dimension $n(n-1)/2$ and that $\mathfrak{o}(n):=\{H\in\mathcal{M}_n(\mathbb{R})\textrm{ s.t. }{}^\intercal H+H=0\}$.
A: You can deduce that $\dim SL(n,\mathbb{R})=n^2-1$ from the fact that $SL(n,\mathbb{R})=\det^{-1}\bigl(\{1\}\bigr)$, together with the fact the the derivative of $\det$ at $\operatorname{Id}$ is surjective.
On the other hand, the Lie algebra of $SL(n,\mathbb{R})$ is$$\mathfrak{sl}(n,\mathbb{R})=\bigl\{X\in\mathfrak{gl}(n,\mathbb{R}\bigr)\,|\,\operatorname{tr} X=0\}\text,$$
since these are the elements of $X\in\mathfrak{gl}(n,\mathbb{R}\bigr)$ such that $(\forall t\in\mathbb{R}):\exp(tX)\in SL(n,\mathbb{R})$. Since $\dim\mathfrak{sl}(n,\mathbb{R})=n^2-1$, $\dim SL(n,\mathbb{R})=n^2-1$.
