The brig is a secure area in a starship or starbase used to accommodate individuals who must be detained - such as criminals or other dangerous individuals. The brig is also used to confine those found guilty of violating the law, or, in the case of Starfleet members, regulations.
The first brigs on Earth Starfleet vessels were small cells with solid doors; later Federation Starfleet vessels and installations utilized force fields to hold inmates which could be switched on or off to allow access. (ENT episode: "Divergence";TOS episode: "Mirror, Mirror")
Galaxy class starships had a number of brig facilities spread throughout the ship. When transporting Kreel and Klingon delegations to DQN-1196, the initial meeting of the two delegations ended in violence. After having most of both delegations stunned, Captain Picard ordered the two delegations held in brig facilities on opposite sides of the saucer section. (TNG novel: Strike Zone)
When the USS Enterprise-D was transporting Kommandant Ghud to his trial on Wyath he was held in the ship's brig, as opposed to guest quarters, at the specific request of the people of Wyath. (TNG - The Space Between comic: "Light of the Day")