From what I understand so far, heads of state generally enjoy immunity from prosecution from the judicial system throughout the duration of their term of office.
To give particular examples, Elizabeth II is immune from prosecution in the United Kingdom, (although this protection does not extend to other members of the Royal family) and the impeachment of President Trump demonstrates the rigmarole of holding the head of state of the United States to account.
Firstly, what is the justification of such a protection? Especially in democracies such as the US and the UK, it seems to me to be counterintuitive that any citizen may be seen as above the law.
Secondly, are there any countries where the head of state is not immune, and in particular, have there been any cases where a head of state has been successfully sentenced while still in office?