What is actually happening behind the code and how should be the code? when we search for a specifc word, will it look in the database (here it is postGIS) and lists the similar words.
What you are talking about is called 'autocomplete'. Google Maps API has the 'Places Library' which provides some of the functionality you are asking about. Read more about that here. However if you are planning on implementing your own using your own database then it is a much more extensive process. A good place for you to start is this guide and also there is always stackexchange questions tagged with autocomplete.