How do you know when a beer is done fermentation and how long does it take before bottling it?
There are various answers to this question depending on how technical one wants to be. The usual and "technically correct" answer is that the brew has finished fermenting when the specific gravity (SG) of the brew remains unchanged over a few days. However there are also things like "stuck fermentations" or weirdly acting yeasts that can be an exception to this rule. Bretannomyces fermentation can take much longer.
Most brewers don't get that specific. They leave the brew to ferment for (say) 10 days to 2 weeks and then check the SG is near to that desired and then (usually) prime and bottle the brew. My personal philosophy is that slightly longer fermentation is better than the quicker option. So bottling anything before 14 days is rarely worth it (unless it is real ginger beer).
However once in the bottle/keg/cask/whatever is is a very good idea to wait at least another 2 weeks before sampling the brew. My personal preference is to bottle condition the brew for at least 2 months. I have many bottles that have been conditioned over 6 months and a few crates that are over a year old. The beer, in the main, just gets better. Lager for instance is often/usually "lagered" (hence the name) for 3 months or so in a cool place before consumption.
There are three main stages of fermentation: Primary, Secondary and Lagering.
Primary fermentation is actually two stages in itself. During the first stage, the yeast performs aerobic respiration, and undergoes a series of changes to adapt to its new environment, hence the name Adaptive Phase. During this stage the main byproducts of respiration are CO2 and water, and the yeast cells develop membranes which are permeable to the sugars and nutrients in your beer. This stage lasts only a few hours.
The next step of primary fermentation is known as the Attenuative Phase. This stage is marked by the growth of a large head of foam at the top of your fermentation vessel known as krausen. This is the part where the vast majority of the sugars in your beer are converted to alcohol. The main byproducts of this stage are again CO2 but also different types of alcohols. This stage will take a varying amount of time depending on the style of the beer and factors such as temperature, but it will typically take at least 2 days but no longer than a week.
By the time secondary fermentation has begun, the majority of wort sugars have already been metabolized by the yeast and the beer will be very close to its final alcohol content. It is marked by an extreme slowdown of the speed of fermentation. At this point many brewers rack or move their beer from the primary fermentation vessel to a secondary one. During this step some of the more complex sugars are consumed by the yeast, as well as some of the byproducts of primary fermentation which would otherwise mar the taste of your beer. The beer reaches its final alcohol content at the end of this phase. This process usually takes around a week or sometimes more.
Calling this its own stage is a bit of a stretch, as it's really the same as the secondary stage. Essentially for lager beers, the secondary stage is drawn out over the course of several months by keeping the beer cool. During this stage most of the yeast settles out of the beer and the byproducts of primary fermentation are broken down.