Women are told to cover their hair and to conceal their adornment because Allah has commanded them to, so as to prevent them from attracting unwanted attention from immoral men.

Allah says in the Qur'an: That will be better, that they should be known (as free respectable women) so as not to be annoyed

By covering their hair, they will not stand out and will not be harassed by corrupt men.

Women should dress modestly in front of unrelated men. At home, however, their awrah is a bit more relaxed, and they are allowed to show what is apparent (e.g. eyes, hands, face).

As for men, it's not obligatory to cover their heads with a hat when praying or otherwise.