When we type w3schools in Google, this is displayed:
It seems like these stuff are called "Google Sitelinks". Why is it that some sitelinks have a search bar while others don't? :
Just like with the Sitelinks Google decides if it will add it or not. The criteria is not public and you do not have any control over whether it will appear or not. Although having built in search functionality in your site probably helps out a whole lot I am sure.