I'll answer for France, but some answers may be field-dependent (I am in mathematics).
1) If I understand correctly, habilitation is the highest academic
degree you can receive and people do it for getting a permanent
academic position in Europe. How many years or how much/many
publication does it normally take to obtain a habilitation degree?
It is the highest academic degree, but in France permanent positions are available earlier: "maître de conférence" (a kind of associate professor) and "chargé de recherche" (same but without any teaching duty) are tenured positions that only needs a PhD. Also In mathematics, I'd say that nowadays people are usually hired within 2 to 4 years after their defense.
Habilitation is need for Professors positions, which are more or less equivalent to full professor positions.
It takes usually from 6 to 12 years to complete a Habilitation (this is probably field dependent, and mathematics are certainly on the junior side).
2) Since you could be admitted as a PhD candidate, but not as a
'habilitation candidate' (but instead, say, as a postdoc) can you
publish in your postdoc and write the paper(s) as a book and submit it
for the defense of habilitation?
Yes, this is common. In fact, usually one even only write a survey of their results and quote the articles. May be strongly field dependent, I do not know.
3) Suppose you do a one year postdoc in university A, and a second in
university B, can you apply to university B for habilitation? How
about university A?
I would say that you would apply to university B. Most people apply when "maître de conférence" or "chargé de recherche" rather than postdocs, but it is not impossible to apply as a postdoc, there are famous examples.
4) If you do your PhD and postdoc in unrelated areas, or say even if
you switch from pure to applied math, would that be a problem for
getting the degree?
Probably not an issue. You'll need to find referees and a jury that complements well if you want to present everything, but usually you do not include your PhD work. I chose not to present my earlier post-PhD work to get a more consistent Habilitation.
5) (Kind of vague question, somewhat opinion-based too) How much does
the chance of getting a European tenure increase if you do a
In France, it would help to get a professor position; if you work abroad I do not think it is mandatory, but good referees report and the composition of the jury can help an application. It would actually hurt an application to a Maître de conférence position, as you would be seen as too senior for the job.
Beware that Professor position are rather rare these years, and that Maître de conférence position do not have an internationally competitive salary (but outside the region of Paris, one lives quite well on it).