We present a theoretical model that computes the chemical evolution, thermal balance, and line emission from the collapsing gas of the envelopes that surround protostars. This is the first attempt to calculate self-consistently the line spectrum from the infalling gas with a model that includes dynamics, chemistry, heating and cooling, and radiative transfer. For the dynamics, we have adopted the Shu "inside-out" spherical collapse of an isothermal cloud valid for r ≥ rc, where the centrifugal radius rc ∼1014-1015 cm. A time-dependent chemical code follows the chemical composition of the envelope during the collapse.The main chemical result is that the inner regions (r ≤ 1O15 cm) have high H2O abundances caused by the evaporation of H2O ice from grains when dust temperatures exceed ∼100 K and by gas-phase chemical reactions when gas temperatures exceed ∼200 K. The gas is heated mainly by absorption of (dust continuum) near-infrared (NIR) photons by H2O molecules in the inner regions, by compressional heating in an intermediate zone, and by collisions of gas with warm dust grains in the outer regions (r ≥ 1O17 cm). The gas is cooled by H2 Orotational lines in the inner regions, by the [O I] 63 μm fine-structure line and CO rotational lines in the intermediate region, and by CO rotational lines in the outer zones. The gas temperature roughly tracks the grain temperature for 1O14 cm ≤ r ≤ 1O17 cm, ranging from about 300 K to 1O K. We present the computed spectrum of a 1 Msun protostar accreting at a rate of 1O-5 Msun yr-1. The H2O lines and the [O I] 63 μm line will be easily detectable by the spectrometers on board the Infrared Space Observatory (ISO). The [O I] 63 μm line and the mid J (J ∼ 7-15) CO lines can be detected by the Kuiper Airborne Observatory (KAO) or the Stratospheric Observatory For Infrared Astronomy (SOFIA), and certain low-J CO lines can be detected by ground-based telescopes. We present also a large number of other models in which we test the sensitivity of the spectrum to the variations in the three main parameters of our model: the inner radius of spherically symmetric infall (e.g., the centrifugal radius), the amount of H2O ice evaporated into the gas, and the mass accretion rate. We show how H2O lines, CO lines, and the [O I] 63 μm line can be used to estimate these three parameters and how resolved line profiles will show the velocity signature of the collapse. Comparison between an infalling and static envelope with similar density, chemical, and dust temperature structure shows that line fluxes alone are not enough to unmistakably distinguish the two cases. Observable H2O masers may be produced in the innermost collapsing gas at r ∼ 4 x 1O14 cm.