Basically, light is emitted when a charged particle moves from a high energy state to a low energy state. In a low density gas, such as your hydrogen lamp, this occurs due to electrons in excited orbits relaxing into lower energy states, so you get molecular hydrogen's emission spectrum. Energy for these emissions is supplied by collisions between electrons and hydrogen molecules.
In a tungsten filament lamp, electrons do not have discrete energy levels but rather exist in a continuous "conduction band", so a continuous spectrum is possible as a continuum of transitions can occur.
For something like the Sun things get more complicated. You get an emission spectrum, but atoms are moving fast enough that there is some doppler shifting of those energy levels so the spectral lines are broadened. The gas is also ionized, so you have additional atomic and molecular species, with different emission spectra superimposed on top of each other, and free electrons which can emit a continuum of wavelengths when their momentum changes through collisions or when they recombine with a nucleus.
I'd recommend reading up on the electronic band structure as understanding this phenomenon is essential to answering your question.
Edit: I realized I didn't cover the issue of the Maxwell Boltzmann distribution. For a material in thermal equilibrium, and neglecting possible quantization of vibrational energy states, kinetic energies of atoms will follow a Maxwell-Boltzmann distribution. So, if energy for your light emission is supplied by thermal vibrations exciting electrons to higher energy states through collisions, and subsequent relaxation of those electrons, then the emission spectrum will be related to the Maxwell-Boltzmann distribution. However, not all light sources rely on conversion of thermal energy to light energy: in fact, this is an extremely inefficient process so it isn't desirable at all from an energy-efficiency standpoint! For light sources that do NOT rely on thermal-to-light energy conversion, the spectrum need not resemble a Maxwell-Boltzmann distribution at all. Case in point: LED lights emit according to the energy gap between the conduction and valence bands. Hydrogen lamps rely on a plasma discharge, where you have hot electrons exciting a cold gas.
So, the spectrum is determined by the energy transitions that occur in the material, and the manner in which these transitions are excited in the first place.