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? :
Webmasters Stack Exchange is a question and answer site for pro webmasters. It only takes a minute to sign up.Sign up to join this community
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.