Most of us have a private network at home and, when configuring it, we are always asked for a subnet mask. Whoa, what's that? And what has a "mask" to do with my tiny network?
Let's start off with some definitions that should be common sense, bu who knows.
For istance, everybody here knows what an IP address is, and even how a LAN works and what are the main problems we may encounter counfiguring one (if not, then google is your friend!).
In a domestic setting, the subnet mask should always be set to the default 255.255.255.0 thus allowing 254 hosts to be connected altogether; in a slightly different reality (that is, a office or some other departement), there's the need for a more complicated and intelligent network configuration, as well as a huge number of hosts connected at the same time.
And here comes the subnetting thing.
You must know (hell, you already should, anyway) that a IP address is 32 bits (8 bytes, yay) long and made up of two components, a network potion and a host portion.
The network address is used to identify the network and is common to all the hosts attacched to the network; the host address is used to identify a specific host (or node) attacched to the network.
IP addresses are usually represented in dotted decimal notation, that is 4 octets in decimal format separated by decimal points.
Classes exist to split IP addresses into 6 major groups, based on their main use.

Class A - The first octet represents the network part, while the other three are used for the host portion; addresses whose first octet is between 1 and 126 are Class A addresses.
Class B - The first two octets represent the network address, while the remaining two are for the host address; any address whose first octet is between 128 and 191 are Class B addresses.
Class C - The first three octets denote the network address, and the last octet is the host portion; if the first octet ranges from 192 to 223 the address belongs to Class C.
Class D - Multicast. Address that are part of this class have their first octet with values between 224 and 239.
Class E - Reserved for future use. First octet ranges from 240 to 255
Note Class A addresses' first octet cannot be either 0 or 127 as these values are used respectively for host identification and loopback facilities.
Also note the maximum amount of addresses available are (2E32)-2.
Sbnetting comes into play whenever we want to divide the network into smaller portions called subn ets. This is done by "borrowing" bits from the host portion of the IP address enabling more efficient use of the network address.
A subnet mask is thus useful to define which portion of the address is used as network identification and which part is the host address.
Every time a frame is delivered to a routing device, the latter does two things:
1. logical AND between its own subnet mask and the destination address; and
2. logical AND between its own subnet mask and the source address.
If the result ofthe two operations is the same, the hosts are part of the same network (router then forwards the frame); else the two hosts are on different networks and it's time to apply some routing procedure to send the frame correctly.
Calculating the subnet mask required for a particular purpose (i.e. number of subnets) is all but difficult.
Once defined the number of subnets desired N (say, 8) and identified the IP address class (say, Class C 192.168.100.x), the very first thing to do is finding how many bits are required N combinations (in our example, 3 -- always choose the upper number if not a power of 2).
Binary speaking, the subnet would be
1111 1111.1111 1111.1111 1111.1110 0000
thus equal to the decimal 255.255.255.224
This is because we borrowed three bis from the host portion for subnetting.
The maximum number of hosts per subnet will then be 2^(32-H)-2 where H is the number of bits of the subnet mask set high (in the example, we set high 27 bits, yelding 2^5 -2 thus 30 hosts per subnet).
At this point, IP addresses can also be written in the form X.X.X.X/H where X and H are respectively decimal values and the number of bit set high in the subnet mask.
For detailed subnet tables showing all possible ways a major network can be subnetted and, in each case, how many effective subnets and hosts are possible, browse http://giulio.hewle.com/gstuff/subnetting.txt