Forecastle refers to the upper deck of a sailing ship forward of the foremast, or the forward part of a ship with the sailors' living quarters. Related to the latter meaning is the phrase "before the mast" which denotes anything related to ordinary sailors, as opposed to a ship's officers.
In medieval shipbuilding, a ship of war was usually equipped with a tall, multi-deck castle-like structure in the bow of the ship. It served as a platform for archers to shoot down on enemy ships, or as a defensive stronghold if the ship were boarded. A similar but usually much larger structure, called the aftcastle, was at the aft end of the ship, often stretching all the way from the main mast to the stern.
Having such tall upper works on the ship was detrimental to sailing performance. As cannons were introduced and gunfire replaced boarding as the primary means of naval combat during the 16th century, the medieval forecastle was no longer needed, and later ships such as the galleon had only a low, one-deck high forecastle.
In addition to crew's quarters, the forecastle may contain essential machinery such as the anchor windlass. On many modern US Naval ships, such as aircraft carriers, the forecastle is the location where boatswain will display their fancy knotwork such as coxcombing.
Some sailing ships and many modern non-sail ships have no forecastle as such at all but the name is still used to indicate the foremost part of the upper deck – although often called the foredeck – and for any crews quarters in the bow of the ship, even if below the main deck.