A Hex nut is a type of fastener with a threaded hole. Nuts are almost always used in conjunction with a mating bolt to fasten multiple parts together. The two partners are kept together by a combination of their threads friction. A slight stretching of the bolt, and compression of the parts to be held together.
The most common shape today is hexagonal, for similar reasons as the bolt head: six sides give a good angles for a tool to approach from (good in tight spots). But more corners would be vulnerable to being rounded off. It takes only one sixth of a rotation to obtain the next side of the hexagon and grip is optimal. However, polygons with more than six sides do not give the requisite grip. And polygons with fewer than six sides take more time to be given a complete rotation. Other specialized shapes exist for certain needs, such as wingnuts for finger adjustment and captive nuts (e.g. cage nut) for inaccessible areas.
Large nuts are often made from steel for strength, and brass hex nuts are often used in general-purpose applications requiring some corrosion resistance. They may be finished with a coating or plating for additional enhanced performance and protection.