Does any one know the reason Stormtroopers always wear helmets everywhere they go? They even wear them in their ships.
I'd also like to know if they are worn when they go to sleep.
Why, because it's part of the on-duty uniform.
The helmet isn't a hat and it doesn't just protect the head, it has many functions including communication and sensors.
From the Wikia
Its reinforced combat helmet featured an integrated comlink, audio pick-up, two artificial air-supply hoses, and a broadband communications antenna powered by a single power cell.
The helmet featured built-in filtration systems that extracted breathable atmosphere from polluted environments.
The helmet's visual processor assisted the wearer in seeing in darkness, glare, and smoke, though it limited the wearer's field of vision.
When firing a blaster, the helmet's visor polarized against the glare. A built-in heads-up display also provided targeting diagnostics, power levels and environmental readings at the corner of the wearer's eyesight, and one could access data on various military subjects and civilian organizations on the helmets display.
In addition, motion sensors alerted the wearer to any enemy the soldier might have missed.
Seeking to discourage nonessential chatter which was strictly off-limits while on-duty, stormtrooper helmets recorded everything that was said by the user, sending it to monitors to review after downloading the data off of the armor's memory.
As for when the Trooper is asleep...there is no reason for them to do so as they would be off-duty.
Out of universe:
It is an example of the faceless goon trope (warning: TV Tropes link)
The reasons for this trope are largely matters of convenience. Most obviously, the faceless goon does not display emotion; hence, he does not display humanity. It's much easier to accept characters as evil (and by extension, feel no tug of sympathy as wave after wave of them get wiped out by the heroes) if you are able to forget that there's actually a human being behind each mask. Not showing any pesky emotions to undercut their menace also adds quite a bit to the creepiness factor.
In universe: Assuming that star wars human psychology is similar to earth human psychology.
Such helmets also provide for deindividuation, which make the storm troopers feel less personal identity and as such less responsible for their actions. It also makes individual storm troopers unrecognizable to civilians and adversities.
In canon, we can see this in the story of FN-2187 (Finn). The deindividuation fails for him. He does feel responsible for his actions. Among the first things he does to assert his individualism is to remove his helmet.
This is in addition to the protection and enhancement that is discussed in the other answers.
Also, because I just answered this in another, almost similar question:
The point is, like with Naval Officers wearing an uniform which looks like this and includes the full uniform, the hat, the rank bar and the code cylinders used to enter restricted space / stuff:
The Stormtroopers are wearing their own uniform which includes a helmet and complete set of armour and are not permitted to remove any of this. I admit, at some points, you see Officers (like at the meeting in EPIV) not wearing their hat, but I assume that's not common when on duty.
Also, to underline my "it is protocol to wear the entire uniform on duty" statement, here is a quote from EPVII:
CAPTAIN PHASMA: FN-2187. Submit your blaster for inspection.
FN-2187: Yes, Captain.
CAPTAIN PHASMA: And who gave you permission to remove that helmet?
FN-2187: I'm sorry, Captain.
CAPTAIN PHASMA: Report to my division at once.
So taking off the helmet is not allowed, because of protocol.
In addition to all of the above, Stormtroopers are generally armed even when indoors.
During my time in the US Air Force, all personnel removed their hats on entering a building - EXCEPT if they were armed.
Armed personnel kept their hats on at all times.
You are carrying a loaded weapon. You shouldn't be dicking around with your hat. You should be concentrating on whatever task it is that requires you to be armed. Removing your hat and keeping track of it distracts you from that task, and from the additional task of keeping that loaded weapon safe.
The "hat on armed personnel" thing also makes it easy to see who is armed in a given (indoor) situation. This is a good thing to know, and can be seen from across a crowded room, even when the weapon itself (often a pistol in a holster) is not visible.
Add to that the communications equipment inside the Stormtrooper helmet, and you can see that it makes sense for on duty Stormtroopers to always wear their helmets.
Because as @Paulie_D states above, they have different built-in modifications. Sensors, communication array and they ofcourse do offer a little protection against physical harm. Even though they were not perfect in every aspect, they seem to help in a variety of situations, following this conversation from Episode VII:
UNDER THE GRATING
Finn pulls the grating over them as Rey works on the controls.
REY: You think this'll work on the Stormtroopers?
FINN: Yeah. Their masks filter out smoke, not toxins.
Protection The helmet is THE armor piece. If you have to fight and you have to chose only one thing to protect, it is the head. Ancient Greeks fought naked with only a helmet.
Features As others have said, there are a lot of features in the helmet for communication, vision, air filtering, limited protection against space vacuum...
Fear It is also a psychological weapon. It's a way to dehumanize the stormtroopers. That way they all look the same. It hides the emotion of the stormtrooper. The expression of the helmet is scary, like the one of the samurai. A battalion of stormtroopers shows a scary face even if the soldier under the helmet is scared to death.
To hide their individualism. To be effective as a mass, one must forego themselves as unique.
SW is a fantasy making up its own rules. It doesn't have to follow Earth's naval traditions.
I fast forwarded Star Wars and didn't see any senior officer, unless you count Vader, in headdress. There's a shot a couple of seconds long just before Leia is escorted to Grand Moff Tarkin that shows what is apparently a junior office on the bridge wearing a cap. He addresses a senior officer and walks off. All junior officers and enlisted, such as the guys who carry the detection gear to the captured Falcon who I assume are enlisted since it looks like they're actually working, wear caps.
In ESB and ROTJ, also on fast forward, everyone in Imperial Service is wearing headdress all the time, senior officers and junior officers and enlisted, notably including the Emperor. Again this is apart from Vader, cheeky little beggar that he is, who manages to be seen in both films without his helmet on.
Maybe Tarkin had a personal dislike for hats that didn't keep his ears warm and so he allowed the same privilege to his senior staff. Tarkin enjoyed his comfort and would wear his slippers while on duty if he thought no one could see. In the next two films Tarkins is dead and we're looking at different imperial units.
If we do want to compare SW to the real world:
The Stormtroopers actually prefer to wear them. They've got no dental plan (the Empire isn't a very good employer). Many of them have had teeth knocked out in training, or on the job. (Lots of them have rather flat noses, too, for the same reason.) For their remaining teeth, well, let's just say that Empire Drill Sergeants spend more time making sure their recruits can shoot than that they can floss properly. On the whole they find it easier to pick up women with the helmet on, than off.