Why is a t-test necessary given that we have the z-test? Can someone give an explanation for why the t-test "happens"? I was taught to use the t-test when you don't know the population standard deviation (i.e., you only know the standard deviation of your sample), but I'm not sure why that would make it different from a z-test.
 A: I don't think I understand your question completely. Are you asking why you would use a t-test?
If you understand why you would use a z-test, you should have a good idea of why you would use a t-test. For large samples, a z-test and a t-test should render similar or identical results. But while a z-test will assume a normal distribution, a t-test will take into account uncertainty in sample distribution at smaller samples sizes. 
A: The z-test itself is actually a likelihood ratio test between the likelihood assuming the null hypothesis and the likelihood assuming the alternate hypothesis. Assuming underlying normal distributions with known variances and only testing the means, the algebra simplifies to the z-test we know and love (DeGroot 1986, pp. 442–447).
Using the same maximum likelihood procedure, but treating the variance as an unknown, creates a different pair of likelihoods and their ratio, and letting the algebra simplify out gives the statistic:
$$
\frac{\sqrt{n}\left(\bar{X}_n - \mu_0\right)}{\sqrt{\frac{S^2_n}{n-1}}}
$$
(DeGroot 1986, pp. 485–489). The testing distribution in question changes as well, as the numerator of the above statistic is normally distributed, $\bar{X}$, and the denominator is distributed as square root of squared normals, the $S^2$, which is the square root of a chi-square random variable. Gosset (Student) showed that if you have a random variable:
$$
Y \sim N(0, 1)\\ 
Z \sim \chi^2_n\\
X \sim \frac{Y}{\sqrt{\frac{Z}{n}}}
$$
then X is distributed with the t distribution and n degrees of freedom.
So, to state it without rigor, the t-test is the natural outcome of the same likelihood ratio process that is behind the z-test when the variance of the data is itself unknown and is being estimated through maximum likelihood.


*

*DeGroot, M. H. Probability and Statistics Addison-Wesley Publishing Company, 1986
A: The non-rigorous answer is that you want to use a t-test when you have a small number of samples because of the chance that the samples are unusually close together (relative to the actual population variance). In that case, the denominator in the formula for the t-statistic will be unusually small, and so the t-statistic itself will be unusually large. Thus, you're much more likely to get a large value for the t-stat when you have a small number of samples than you would be to get a comparably large z-stat, so you need a larger value to reject the null using the t-test than the z-test at the same significance level.
A: The most important differentiator is sample size, as a rule of thumb: If $n$ is smaller than $30$ a t-test should be used, otherwise a z-test.
A good overview of the underlying assumptions and differences (and similarities) of both tests is given here:
http://www.le.ac.uk/bl/gat/virtualfc/Stats/ttest.html
