When all available seats in a given train are sold, Indian Railway or IRCTC release RAC for booking, once those are sold as well, then WL tickets are released.
For every train people naturally cancel their tickets for various reasons. It is those cancellations that can get you on the train if you bought a RAC or WL ticket. When the actual reservation chart of the particular train is ready (that means the trains are due to depart in a few hours usually) and there are still some available tickets is different quotas, those will be given to RAC/WL ticket holders.
Here is an example of a booking situation. Tickets are sold in following order: