Gauge Bosons
Mass terms for any gauge bosons are forbidden since they are not invariant under gauge transformations. Suppose you have some symmetry $ SU(N ) $ with generators $ T ^a $. To be a symmetry there must be a set of gauge bosons which I denote $B _ \mu ^a $. The mass terms for these bosons are
\begin{equation}
- m ^2 B _ \mu ^a B ^{a, \mu}
\end{equation}
The transformation of the gauge boson is
\begin{equation}
B _\mu ^a \rightarrow B _\mu ^a + \partial _\mu \alpha ^a + g \epsilon _{ a b c } B _\mu ^b \alpha ^c
\end{equation}
It's easy to see that under a gauge transformation the mass term is not invariant. Since such terms doesn't respect the gauge symmetry we say that they are forbidden and cannot be written down when trying to writing a $ SU(N) $ gauge theory. Thus any gauge boson must be massless.
Now there is a small loophole in this arguement. Suppose that the gauge boson also couples to some scalar particle (in the Standard Model this is the Higgs) and that this particle happens to have a non-zero vacuum expectation value. Then at low energies the symmetry will appear to be broken. The apparent breaking can is what gives gauge bosons their masses.
Explicitly, in the Standard Model (SM) we have terms as,
\begin{equation}
\frac{ g ^2 }{ 2} \phi ^\ast \phi W ^a _\mu W ^{a,\mu }
\end{equation}
where $ \phi $ is the two component Higgs field with the first component being charged and second neutral. The Higgs field has a neutral vacuum expectation value and its value is typically around $ 246 \mbox{GeV} $ however it can be slightly smaller or bigger then this value. We write,
\begin{equation}
\phi = \frac{1}{\sqrt{2}} \left( \begin{array}{c}
0\\
h (x) + v
\end{array} \right)
\end{equation}
Inserting this relation we find that near the vacuum (which is also the point the system is typically near) we end up with an extra mass term:
\begin{equation}
\frac{ v ^2 g ^2 }{ 2} W _\mu ^a W ^{ a , \mu }
\end{equation}
This term arises since the symmetry was spontaneously broken but we could not have written it otherwise.
Fermions
Fermions cannot get masses in the SM for a similar reason. A fermionic mass term for some Dirac field $ \Psi $ is given by (fermions could also in principle have masses called Majorana masses however, these break all gauge symmetries),
\begin{equation}
- m \bar{\Psi} \Psi
\end{equation}
If the field is only charged under a $ U(1) $ charge then it is indeed gauge invariant. However, if the field is charged under another symmetry that transforms between different fields then it is not invariant and is again forbidden (note that if the new symmetry is left-right symmetric and the new fields have the same mass as $\Psi$ then such terms could still be allowed but this is not the case in the SM).
Explicitly, in the SM in QED we are allowed to write the mass term for a field $ e $ as:
\begin{equation}
- m \bar{e} e = - m \left( \bar{e _L } e _R + \bar{ e _R } e _L \right)
\end{equation}
where $ e _{ L/R} \equiv P _{ L/R } e $. Now in the SM, each fermion is also charged under $ SU(2) $. The $ SU(2) $ symmetry transforms the field $ e _L $ into another field $ \nu _L $. Under such a trasformation the mass term is clearly not invariant and is forbidden.
Just as before one can use the Higgs mechanism to save the day. If the Higgs is also a doublet under $ SU(2) $ as in the SM then we have a term that is a product of the Higgs and the $ SU(2) $ doublet of $ e _L $ and $ \nu _L $:
\begin{equation}
- g\left( \begin{array}{cc} \bar{ \nu _L} & \bar{ e _L } \end{array} \right) \left( \begin{array}{c}
\phi _1 \\
\phi _2
\end{array} \right) e _R + h.c.
\end{equation}
After the Higgs mechanism we have the term,
\begin{equation}
- \underbrace{\frac{g v}{\sqrt{2}}}_m ( \bar{ e _L} e _R + h.c.)
\end{equation}
as desired.