Identically vanishing trace of $T^{\mu\nu}$ and trace anomaly Let us consider a theory defined by an action on a flat space $S[\phi]$ where $\phi$ denotes collectively the fields of the theory. We will study the theory on a general background $g_{\mu\nu}$ and then we will set the metric to be flat.
The Euclidean partition function of the theory in the presence of an external source is
$$
Z[J] = \int [d\phi] e^{-S -\int d^d x\, J \, \mathcal{O}}\tag 1
$$
where $ \mathcal{O}$ can be either an elementary or a composite field (in what follows we will take it to be the trace of the energy-momentum tensor).
Now, a very well-known result is that a traceless energy momentum tensor implies conformal invariance; indeed, under a conformal transformation $g_{\mu\nu} \rightarrow f(x)g_{\mu\nu} $  such that
$$
\partial_{(\mu}\epsilon_{\nu)} = f(x) g_{\mu\nu}
$$
the action transforms like
$$
\delta S = \frac{1}{d}\int d^d x T^{\mu}_\mu \partial_\rho \epsilon^\rho
$$
Now, another well-known result states that in a generic background metric the expectation value of $T^\mu_\mu$ is not zero but depends on the Weyl-invariant tensors and the Euler density, that is
$$
\langle T^\mu_\mu \rangle = \sum a_i E_d - c_i W_{\mu\nu\rho....}^2
$$
where $\langle T^\mu_\mu \rangle$ is usually defined by the variation of the connected vacuum functional $W = \log Z[J]$ under variations of the metric.
First question. Is $\langle T^\mu_\mu \rangle$ calculable in the usual way using the partition function? That is setting $\mathcal{O} = T^\mu_\mu $ in Eq.(1) we compute
$$
\langle T^\mu_\mu \rangle  = \frac{\delta}{\delta\, J} Z[J]\Big|_{J=0}\tag 2
$$
Second question. If the answer of the first question is YES, then I would expect the $\langle T^\mu_\mu \rangle$ computed as the variation of the connected vacuum functional $W[J]$ is the same as the one computed in Eq.(2). Is this true?
Third question.
There are two ways the classical traceless condition can be realized:


*

*on-shell; then, $T^{\mu}_\mu$ is not identically zero but it is so once you apply the equation of motion, e.g. $\lambda \phi^4$ theory in d=4.

*$T^\mu_\mu$ is identically zero; that is, you don't need to use the equation of motion (e.g. massless scalar field in d=2 on a curved background)


In the first case, since $T^\mu_\mu$ vanishes on the equation of motion, I agree that it may receive quantum corrections through the coupling of the theory to a curved space; everything is ok.
In the second case instead, namely $T^\mu_\mu$ identically zero, I am unable to compute its expectation value from Eq.(1) and Eq.(2) since $\mathcal{O}=0$ identically; that is, the RHS of Eq.(2) is zero, since  Z[J] is actually J-independent. This would imply $\langle T^\mu_\mu \rangle=0$.
Is it still true that the theory enjoys an anomaly on a curved space background? I would say YES, since the anomaly depends only on the central charges. How to resolve this apparent contradiction?
 A: The answer to your first two questions is positive: $\langle T^\mu_\mu \rangle$ can be computed from the partition function, and it is the same as the variation of the connected vacuum functional $W[J]$. I'll let someone else go into the details of a proof, if needed.
Then there is a bit of confusion in your third question: the fact that there is an anomaly tells you exactly that $T^\mu_\mu$ is never identically zero in curved space background (except maybe in very special cases, but then there is no anomaly).
The terms that you wrote in your anomaly, i.e. the Euler density and Weyl tensor squared, are curvature tensors that vanish in flat space. So you will always find $\langle T^\mu_\mu \rangle = 0$ with this anomaly.
But this does not yet mean that $T^\mu_\mu$ is identically zero: you could still have 
$$
\langle T^\mu_\mu \mathcal{O}_1 \cdots \mathcal{O}_n \rangle \neq 0.
$$
The question whether all such correlators vanish in a theory in which $\langle T^\mu_\mu \rangle = 0$ is still not completely resolved in dimensions $d > 2$.
If you want to read more on the subject, I suggest looking at
https://arxiv.org/abs/1302.0884 and references therein.
