A subnet is a subset of an IP network address space.
In its inception, IPv4 featured classful addressing. This meant that all networks needed either a Class A, Class B, or Class C block of addresses. In these address blocks, the first address in numerical order is the network ID, the last address in numerical order is the broadcast address, and the remaining addresses are for hosts.
The table below shows essential data about classful IPv4 address blocks.
Notice that the final column, number of hosts per network, shows the crucial limitation of classful addressing. Most networks do not fit neatly into fixed address blocks with a minimum 254 hosts or with increments into the thousands or the millions.
Because of these limitations, variable length subnet masks (VLSM) were introduced, allowing for the number of network bits to vary as high as 30. Given a number of network bits, N, the remaining 32 - N bits determine the range of the host address space. Subnets are essentially subdivisions of some classful IP address space into smaller, more flexible, classless spaces.
Subnet calculation is a tricky process (arguably the most difficult subject in the networking curriculum), but the system is deterministic in a way that allows for subnet calculators to assist with the process. As a general rule, any time a host bit is "borrowed" for use as a network bit, the number of subnets doubles, and the size of each resulting address block is cut roughly in half. Roughly, not exactly, in half, because each subnet requires its own network ID and broadcast address. The subnetting process, therefore, incurs some overhead, with host addresses being given up for network and broadcast each time further subnetting is undertaken. In the extreme case, /30 (equivalent to a subnet mask of 255.255.255.252), fully half of the subnet address numbers are used for network IDs and broadcast. The table below shows the resulting subnet sizes for Class C networks only.
Similar calculations will result in tables like this for Classes A and B. A useful relationship to note is that the available host address space = 2^H - 2, where H is the number of host bits. So for some desired number of host addresses, a little algebra (or inspired guesswork) can determine the number of host bits H that will yield the right-sized subnet. From H, the values for all other variables - such as N (the network bits), the VLSM number, and the subnet mask - can be determined.