Assuming bi-polar encoding, and assuming that the number of transmitted 1's is equal to the number of 0's, then over very long time intervals the positive pulses "cancel out" the negative pulses and the average voltage is zero.
However, over smaller time intervals, there's likely to be more 1's than 0's, or vice versa. This is what introduces a DC component to the signal.
Intuitively, you may think about it this way: if you filter the line-encoded signal with a very narrow low-pass filter, you'll see a non-zero, varying output. The DC value is the expected value of the filter's output.
In order to reduce or eliminate DC you need a line encoding that forces the number of positive and negative pulses to be the same, even over very short time spans. One example is the Alternate Mark Inversion (AMI) encoding.