Ah, this could be the subject of pages and pages of writing.

(Having done some growing recently, (and been involved in more over the years) I flatter myself that I have a good understanding of the process.
So...in short...once the vegetative stage is complete, (whether due to age or change in light), the flowering stage begins. At this point, it is possible to distinguish between male plants (which develop stamens) and female plants which develop "buds" (pistils).
The trick is to remove the male before pollination can occur, and hence inhibit the development of seeds (and the concomitant waste of energy that could be spent on THC development) in the female.
Below I present the result of my last attempt, a
Cannabis Indica strain dubbed "Blue Mystic." (Perfectly legal to grow for personal use in South Africa btw.)
--A