Bail is a mechanism designed to "reasonably assure the appearance of the person before the court." In other words, it is collateral that guarantees that the defendant shows up for each future court appearance.
Bail can be an amount of money (cash bail) or a defendant's promise to appear (personal recognizance).
In Massachusetts, there is a "presumption of release on personal recognizance." (See G.L. c. 276, § 58). That presumption can be overcome by evidence that the defendant is not likely to reappear unless there is additional incentive to do so. The amount of bail is set by the Judge. A judge may consider numerous factors in determining the bail - the nature and circumstances of the crime alleged, potential penalty if convicted, the defendant's criminal history, the defendant's history of defaults, a defendant's ties to the local community, a defendant's drug/alcohol dependency, etc.
The defendant's attorney typically argues for a low amount of bail, an amount that the defendant (or defendant's family) can pay. The prosecutor often argues for a higher amount of bail. Though, oftentimes, the prosecutor and defense attorney agree on an amount.
For example: Bob - 30 years old, 1 child, wife, full-time job, resided in same town for 10 years, two previous convictions, history of defaults - is charged with Distribution of Cocaine, Subsequent Offense. At the arraignment the Commonwealth asks for $2000 bail; the prosecutor cites the mandatory-minimum sentence faced by the defendant, his criminal record and numerous defaults. In opposition, the defense attorney asks that the defendant be released on personal recognizance; she cites the defendant's strong ties to the community, the fact that he lives with his wife and child, and his employment status, as incentive for him not to flee. After hearing the arguments, the judge sets bail at $1000. Luckily, for Bob, his wife can obtain the money quickly. She goes to the bank, withdraws the cash, comes to court, and pays the bail. If the bail was not paid, Bob would have stayed in jail until the case is over.
The person who posts the bail on the defendant's behalf is called the "surety." The surety posts bail at a risk, because if a defendant fails to appear the court may decide to "forfeit" the bail. In other words, the court takes the money forever. The surety will not be able to get the money back.