From the Super Mario Wiki
“Luigi, I found this cap in front of the castle a little while ago. You must have dropped it. If you want to become a full-fledged hero like your brother, you can't be so careless!”
Luigi's hat color was taken from the Shellcreeper's palette to distinguish Mario from Luigi. The hat was made green with a green "L" emblem, but it was white on Super Mario Bros. and Super Mario Bros: The Lost Levels.
In Super Mario Bros. 2, the tip of the cap had been changed to black instead of its usual green. This happened for Mario's cap as well. However, after this game, Luigi's cap reverted to having a green tip.
In Super Mario 64 DS, Luigi's hat was used to shape-shift into Luigi if the player was either Mario, Wario, or Yoshi. In the same game, Luigi is affected in the same way as Mario and Wario if his hat is lost. Luigi's defense and attack sharply decline while his hat is absent from his head. If the player gets the hat back, Luigi's attack and defense revert to normal. Until Luigi is unlocked, it only appears in Big Boo's Haunt, on any star except the first one.