Microscopic theory of the semiconductor terahertz response