The offside rule is a common regulation in many team sports to restrain players to certain areas of the field or court.

The purpose of the rule is to prevent teams from spreading their players in an effort to remove all risk and daring from the game. For instance, basketball employs an over-and-back rule, which limits the area in which the offensive team can dribble and thus prevents a boring game of keep-away.

Most offside rules limit offensive players from lingering too close to their opponent's goal. This actually encourages offensive play, for if attacking players could roam wherever they wished, the defense would fall back to the goal line and the game would be quite boring.

Now, let's look at offside rules, sport-by-sport. I've listed any team sports (with more than two players per side) that are either recognized by the Olympics or are popular in certain countries. I'm not counting baseball, cricket, takraw and volleyball, because those aren't games of field position. As for rules, I'm going with the sport's international organization (such as FIBA for basketball).

Basketball
As I said before, basketball's version of the offside rule is called the over-and-back rule. Once the offensive team has brought the ball over the halfway line toward their opponent's basket, that team cannot cross back over the line with the ball. If you are holding the ball with one foot on each side of the line, you're safe, but if you have both feet on the wrong side of the court, then the other team gets possession. Two caveats: There are no over-and-back violations on inbounds plays, and if an opponent knocks the ball across the halfway line, you're allowed to go back and get it.

Moreover, basketball also has rule to prevent offensive players from camping out underneath the basket. This is called a lane violation; in it, an offensive player cannot keep both feet in the painted area underneath the basket (otherwise known as the key) for more than three seconds if he does not have the ball. That is, he must have at least one foot outside the key every three seconds. In the NBA, there is also a five-second lane violation rule against defensive players, but FIBA does not enforce this. In either case of lane violations, the other team gets to inbound the ball.

Football (American and Canadian)
Before a play can start, the referee lays down the football on the field with each end of the ball pointing at an end zone. All the defensive players must be in front of the ball, and all the offensive players must be behind it — no one can cross the "invisible line" extending across the width of the field except for the center, who must touch the ball with his hand to begin the play. If someone is breaching this line when the ball is snapped, it's offside, and that team is penalized five yards.

Football (Association/Soccer)
Many writeups above this one are solely devoted to the soccer offside rule, and for good reason — it's complicated. Basically, offensive players must always make sure that there are at least two defensive players between them and the goal. So, if I'm 30 meters away from the goal, and there is a goalkeeper and another defender five meters away from the goal, then I'm okay. A violation of this rule results in a free kick for the defensive team.

However, with this rule, there are a lot of caveats. (1) There can be no offside on your half of the field. (2) There can be no offside if you are in possession of the ball, or if the ball is nearer to the goal than you are. (3) There is no offside on a throw-in play, but there can be offside immediately afterward. (4) If you are receiving a pass, and if you are onside when the ball leaves the foot of the passer, then you cannot be offside, even if you run past all the defenders to catch up to the ball. So, if you're barely onside when a teammate kicks the ball hard down the field, you're allowed to run "offside" even when the ball is still behind you in transit. (5) If you are offside but not taking part in the play at all, then the referee may decide to let the game continue. This is totally at his discretion. However, if the defense kicks the ball toward this offside (but out-of-the-way) player, then he is considered to be put onside by the defense's mistake. (6) No offside on back passes: If you have the ball and are ahead of the defense, and then you kick the ball "backward" to a teammate, then you won't be called for offside as long as you remain out of the play. As a corollary, there is never offside on a corner kick. Any corner kick is technically a back pass, since it originates from the goal line.

Football (Australian)
Has more in common with rugby than with American or Canadian football, to say nothing of Association football. Anyway, there's no offside.

Hockey (Field)
No offside. It's rather difficult to hit the ball a long distance with any accuracy, considering how awkward to use field hockey sticks are, so the sport isn't worried about offenses spreading themselves out too much.

Hockey (Ice)
A hockey rink has two blue lines parallel to the goal line that divide the rink into three sorta-equal sections. The two areas with the goals are called the attacking zones, and the (smaller) area in the middle is called the neutral zone or center ice.

Now, the offside rule states that an offensive player cannot cross the blue line into the attack zone unless he's carrying the puck or if the puck is there ahead of him. (The latter ploy is known as "dumping the puck" or as "dump and chase," because the offense slaps the puck into the corner and then has to chase it down.)

If a player is offside — that is, if he is in the attacking zone and the puck isn't — then the game will continue, though a linesman will signal that there is an offside pending. If a teammate of the offside player then puts the puck into the attacking zone, a whistle will be blown to stop the game, and there will be a face-off in the neutral zone. However, if the defense hits the puck into their own zone, then the offside is waved off, and the game continues uninterrupted.

Ice hockey has another restraining rule as well; this is called the two-line pass, or sometimes the offiside pass. There are three parallel lines in the middle of a hockey rink — the two blue lines I told you about before, as well as the red center line — and a pass cannot cross a team's defensive blue line (the one closest to its goal) as well as the center line. In effect, "long" passes are forbidden. (Note: In the 2002 Olympics, hockey decided to allow two-line passes, and the result was fun, open play. It remains to be seen if they'll be allowed for 2006.)

Hurling
A rather violent Irish game. No offside.

Lacrosse (Men's)
Like in ice hockey, a lacrosse field is split into three more-or-less equal areas. In lacrosse, however, each team must keep four players (goaltender included) on its defensive end and three players on its offensive end. Three players, called midfielders, are free to roam wherever.

The rule doesn't care which players remain in the proper ends, just as long as the correct numbers stay there. So if a defenseman picks off an errant pass and starts running down the field on a breakaway, then one of the midfielders has to hang back. A violation of this rule results in a 30-second penalty if the defensive team is at fault, or a change of possession if the offensive team committed the no-no.

Lacrosse (Women's)
Women's lacrosse also has three zones, but they're a little bit more lax about who goes where. (Pardon the pun.) Anyway, only seven offensive players and eight defensive players, including the goaltender, can go into each attacking zone. A violation results in a free possession for the other team.

Polo (Water)
The offside rule in water polo is only in effect within two meters of each goal. An offensive player cannot enter the area unless the player has the ball, or unless the ball is already in the area. Back passes are legal, as long as the receiving player shoots the ball immediately, or if the passing player leaves the two-meter area as quickly as possible. If a team is penalized for being offside, its opponent gets a free throw, which is a free possession from the spot of the foul.

Polo (Ponies)
No offside penalty. Give them a break, they're horses.

Rugby
In rugby union, offensive players who do not have the ball must remain behind the ball-carrier, as well as the person who last carried the ball. If a person is off-side, he will not be penalized if he does not take an active part in the play. Moreover, an offside player can be put onside by the action of himself or his teammates. For instance, if I'm offside, I can run behind the ball carrier to get back onside.

Kicks are a whole 'nother category. If you're offside on a kick, then you cannot run toward the ball unless you're put onside. This can happen if ... (1) the kicker runs past you; (2) a teammate who was behind the kicker runs past you; (3) an opposing player catches the ball and runs for at least five meters; or if (4) an opposing player drops the ball. Also, if you're offside and the kicked ball happens to land near you, then you must retreat to a distance of 10 meters from the ball until you're put onside again.

The penalty for an offside call is either a free kick or a scrum at the position which the foul occurred.

Computer programming
It's true, it's true. According to longtime programmer Peter Landin, any program that delineates declarations with indentations is considered to follow the "offside rule." Examples include Miranda and Haskell.

Sources:
Field Hockey: http://www.usfieldhockey.com/hockey/fih/fih_contents.htm
Men's Lax: http://www.lacrosse.org/the_sport/mens_rules.phtml
Women's Lax: http://www.lacrosse.org/the_sport/womens_rules.phtml
Rugby: http://www.rfu.com/index.cfm/fuseaction/RFUHome.Refereeing_Detail/StoryID/382
Water Polo: http://www.fina.org/waterpolorules_20.html
Programming: http://wombat.doc.ic.ac.uk/foldoc/foldoc.cgi?offside+rule
Plus Great Neb's writeup on rugby offsides.
Thanks to unperson and Ian_Bailey for help on basketball and ice hockey, respectively.
Big props to kthejoker for clearing up soccer and ice hockey, plus that computer programming nonsense :)