Is it common to offer a position to someone before the position is advertised? I have been offered and have accepted a position at a university. The position is not advertised yet, but they will advertise it because they are required to. Is this something common in academia? To be more clear on what happened, prior to the offer I sent my CV as a respond to another similar position which they advertised, but it was to be filled immediately and so I was ruled out. I guess after looking at my CV and having further conversations with me they were keen for me to join them and so they offered me this new position. But because they are required to, they have to go through another round of formalities. 

I did ask the head of department how he could be sure that I would be the most suitable person for the job and told him that others might be better candidates, to which he said something to the effect that he saw me as suited for the group. But since I have not signed any contracts yet, how can I guarantee that I will eventually get the job? From the time I accepted the offer, I have stopped looking for other jobs, and have withdrawn my other applications, but since there are no contracts signed yet, I am a bit worried. 

Perhaps another question I should ask is why are universities required to put up advertisements and to go trough all the formalities if they already decided on someone?
Offering a position informally is illegal, but unfortunately still common practice in academia in the UK and some other countries. By offering you this position, the University violated good business practice and possibly breached several laws (although I am not a lawyer and can not speak in confidence here).

From purely human perspective, consider other candidates, who will find this post advertised online, will spend hours and days preparing and polishing their applications, put their time and effort in this work. Some of them will eventually be shortlisted, and will come to the interview, some of them travelling from other countries in hope to get a better job. And their desires and hopes will be for nothing, because this position is already promised to you, and all the process is in fact fake.

You describe your sense of insecurity. But for other candidates it could also be a very frustrating experience to be selected for an interview and then turned down for no obvious reason, often without a decent feedback or even a polite letter of rejection. 

Finding a job in academia is increasingly difficult, but this is exactly why fair hiring practice is now greatly important to ensure that the best candidates have the positions of the base of their merit. Although you may have no better options than to join the University which offers you the position, it is important to realise that their current practice is toxic and should be improved.