Because of overnight news and traders sentiment, the stock will have a huge demand or supply the next day morning when market opens.
In olden days, this kind of sudden urge to buy or sell the stock exactly at 9:15 am had lead to severe spikes and very uncertain prices during the start of the trading sessions.
Hence NSE/exchanges decided to have a pre-opening session in which they will check the view of the traders either bullish or bearish and arrive at a opening price based on the orders placed by traders between 9:00 to 9:07 am. When the traders are bullish, they quote for very high prices than the previous day’s closing price and most of the orders would lie on higher price side, and so the opening price will GAP UP and open at a higher price than previous day’s close. When the price opens GAP UP most orders will lie around that range and will get executed quickly without any big fluctuation in stock price when the market resumes normal trading at 9:15 am.
Same is applicable for GAP DOWN when traders are bearish.