Most sailors who go out on deployment don't get into trouble. Others may find themselves on the wrong side of the shore patrol, though. Much of that can be minor, and is usually addressed with a loss of pay, or placing a sailor on restriction. But in some cases, that sailor needs to be confined.
Now, when you're deployed to the Middle East, Mediterranean, or some other hot spot, it's hard to ship the guy (or gal) back to the States to lock them up. So, on carriers and other large ships, the jail is brought with them – and it's called the brig.
And in case you think that an upcoming battle earns some leeway for misbehavior, you'd best keep in mind that heading towards a fight won't keep a sailor from getting tossed in the brig. In the book "Miracle at Midway," historian Gordon Prange related how Marc Mitscher, captain of the aircraft carrier USS Hornet (CV 8), threw a couple of sailors in the brig for minor infractions prior to the Battle of Midway.
In many cases where that is necessary, the sailors are sent to the brig after what is known as a "Captain's Mast," which is covered under Article 15 of the Uniform Code of Military Justice. According to Naval Orientation, the amount of time someone may be confined is limited. The exact limits depend on the rank of the commanding officer and the rank of the accused. The chart below from the linked manual explains those limits.
The video clip below is from the 2008 documentary mini-series "Carrier," produced by Mel Gibson's production company. It provides a tour of the brig on the nuclear-powered aircraft carrier USS Nimitz (CVN 68) as it was in 2005.