From the Super Mario Wiki
Mario was given his hat in his first appearance in Donkey Kong because of the limited advances in video game graphics when the game was made. A red cap was added because programmers found it difficult to create the hair movement that would occur when the character jumped.
Super Mario series
Super Mario 64 / Super Mario 64 DS
The hat has appeared with Mario ever since Donkey Kong, but was unimportant until Super Mario 64 - the first game in which Mario can lose his hat. If his hat is lost (having been stolen by a Ukiki or Klepto or blown off by a gust of wind, for instance), Mario takes more damage when attacked.
In the remake of Super Mario 64, Super Mario 64 DS, Toad reveals that the cap Mario wears is special, and that if it were to get blown off of Mario, he would have nothing but bad luck. As hinted in Super Mario 64 DS, Mario's Cap may be a source of his strength, though this does not apply in other games where Mario can have his hat off. If Mario loses his cap, he can get it back by going back to the course in which he lost it to find it. However, if he instead gets a Power Star from another course, he can simply talk to Toad (who will find and wear Mario's Cap) and get it back from him. Mario's Cap appears in the following courses, even when he is locked: Bob-omb Battlefield (Star 2 or 4), Whomp's Fortress (Star 1 or 2), Jolly Roger Bay, and Cool, Cool Mountain (Star 2). Mario's Cap can also be found in most levels when playing as Yoshi. Yoshi, Luigi, and Wario can pick up the cap, which gives them the appearance and abilities of Mario (though they retain their voices) until they lose it, get hurt, or finish a level.
Super Mario Sunshine
The same effects of Mario's Cap in Super Mario 64 hold true in Super Mario Sunshine, but with the added effect of Mario gradually losing health due to the heat. The only enemies that steal Mario's Cap in this game are Swipin' Stus.
Super Mario Galaxy 2
The cap's importance to Mario is stressed again at the end of Super Mario Galaxy 2, when Luma takes it with him as a souvenir before returning to the Comet Observatory with Rosalina, and Mario appears shocked to no longer have it. Following this, Mario is seen capless throughout the credits, but regains his cap when he is playable again, presumably getting a spare cap. The same thing happens when the player is Luigi.
Super Mario 3D Land
In Super Mario 3D Land, if Mario and Luigi are reduced to their small forms, they won't wear their caps. However, once the player gains 1110 extra lives, Small Mario and Small Luigi will have their hats on, but their regular forms won't.
New Super Luigi U
Super Mario Odyssey
In Super Mario Odyssey, Mario's cap appears to have become sentient to some extent, sprouting eyes and even moving at one point. A new mechanic in the game is Mario throwing the cap like a boomerang, and even jumping off of it in midair like a platform.
Luigi's MansionLuigi's Mansion, Mario's Cap is one of the five items that Mario lost, and Luigi must bring it to Madame Clairvoya to receive information regarding Mario's whereabouts.
Mario Golf: World Tour
In Super Mario 64, Mario makes use of three different types of caps in addition to his regular cap: the Wing Cap (which enables Mario to fly), the Metal Cap (which enables Mario to walk underwater and through fire), and the Vanish Cap (which enables Mario to walk through certain walls). While these powers are dispersed amongst Mario and his companions in Super Mario 64 DS, Mario's Cap gains a new ability: when worn, it turns Luigi, Yoshi, or Wario into Mario. If Mario loses his cap, he can't fly, use other caps, or use Power Flower abilities.
Due to certain glitches in Super Mario 64, it's possible for Mario to end up with more than one cap; the duplicate can be used as an effective weapon. In Super Mario 64 DS, if someone wearing Mario's Cap gets another cap, they get an extra life. However, if the real Mario gets another one of his caps from a certain glitch in Snowman's Land, he'll pick up the cap and nothing will happen.
In New Super Mario Bros. Wii, an Easter egg allows Mario to take off his hat when he gets 99 lives, though he still does the victory animation at the end of a level as if his hat were still there. This is seen again in New Super Mario Bros. 2 after collecting 1110 lives. In Super Mario 3D Land, Small Mario will get a hat when reaching maximum lives while Super Mario will take it off. When Mario collects almost any power-up, his cap will change color.