Note that under an infinitesimal change in the metric of the form $g \to g + \delta g$ the action changes to
$$
\delta S = \int T^{ab} \delta g_{ab}
$$
Now, under Weyl transformations we have
$$
g_{ab} \to e^{2\omega} g_{ab} \qquad \implies \qquad \delta g_{ab} = 2 \omega g_{ab}
$$
For Weyl transformations $\omega$ is completely arbitrary. If we consider a conformal transformation then, the metric also transforms as above except that $\omega = \frac{1}{d} \nabla_a \xi^a$ where $\xi^a$ is a conformal killing vector, i.e. $\omega$ takes a specific functional form.
Either way, for both conformal or Weyl transformations $\delta g_{ab} =2\omega g_{ab}$. Thus, for either of these transformations, the variation in the metric is
$$
\delta S = 2 \int \omega T
$$
Thus, if the trace of energy momentum tensor vanishes, $T = 0$, then
$$
\delta S = 0
$$
and we have a symmetry of our theory!
OK. So we have shown that if $T = 0$, then the theory is invariant under Weyl and conformal transformations. What about the inverse statement? Can we infer from Weyl and conformal invariance that $T = 0$? The latter is a more subtle question.
Weyl or conformal invariance implies
$$
\int \omega T = 0
$$
Now, when talking about Weyl invariance, the above is true for arbitrary $\omega$. In this case, we can most certainly conclude that $T = 0$ (for instance take $\omega \propto \delta^4(x)$ or some smoothed out version thereof and we immediately reach this conclusion.
When talking about conformal invariance, $\omega$ is not arbitrary and we cannot conclude that $T$ must vanish. For instance, in a flat background, $\omega$ takes the form $\lambda + a_\mu x^\mu$ where $\lambda$ and $a_\mu$ are arbitrary constants. Thus, all we can conclude is that we must have
$$
\int T = 0 ~, \qquad \int x^\mu T = 0
$$
These two conditions no longer imply that $T = 0$. Thus, as per this argument the inverse statement is not necessarily true in conformal field theories. I'm not sure if there is any other argument that can be used to justify that $T$ must vanish in CFTs, but so far, all the CFTs we study have $T = 0$.