There is a halacha that men should wear hats when they daven as a sign of
respect, just as one would wear a hat when visiting a king. The wearing
of "black" in particular as well as wearing it in the street and all over
is a custom that has grown from different societies. My Rosh Hayishiva
once explained to me the wearing of a hat today even though no one else
in society wears one in this manner: almost all people who wear uniforms
i.e. police, fire baseball players etc., have a hat as part of their
uniform; So too, Bnei Torah have a uniform and it includes the wearing of a
hat.
Dovid Rockove