A complete reassessment of the HST observations of the transits of the extrasolar planet HD209458b has provided a transmission spectrum of the atmosphere over a wide range of wavelengths. Analysis of the NaI absorption line profile has already shown that the sodium abundance has to drop by at least a factor of ten above a critical altitude. Here we analyze the profile in the deep core of the NaI doublet line from HST and high-resolution ground-based spectra to further constrain the vertical structure of the HD209458b atmosphere. With a wavelength-dependent cross section that spans more than 5 orders of magnitude, we use the absorption signature of the NaI doublet as an atmospheric probe. The NaI transmission features are shown to sample the atmosphere of HD209458b over an altitude range of more than 6500km, corresponding to a pressure range of 14 scale heights spanning 1 millibar to 1e-9 bar pressures. By comparing the observations with a multi-layer model in which temperature is a free parameter at the resolution of the atmospheric scale height, we constrain the temperature vertical profile and variations in the Na abundance in the upper part of the atmosphere of HD209458b. We find a rise in temperature above the drop in sodium abundance at the 3mbar level. We also identify an isothermal atmospheric layer at 1500+/-100K spanning almost 6 scale heights in altitude, from 1e-5 to 1e-7 bar. Above this layer, the temperature rises again to 2500(+1500/-1000)K at 1e-9 bar, indicating the presence of a thermosphere. The resulting temperature-pressure (T-P) profile agrees with the Na condensation scenario at the 3 mbar level, with a possible signature of sodium ionization at higher altitudes, near the 3e-5 bar level. Our T-P profile is found to be in good agreement with the profiles obtained with aeronomical models including hydrodynamic escape.