Every stock broker opens a bank account with a Clearing Bank for settlement of funds with the exchange. So when any client of the broker buys stock, the money is debited by the exchange from this account, and when someone sells stock the credit of funds is given to this account. NSE presently has around 13 banks empanelled to be clearing banks.
There are two depositories in the country NSDL and CDSL, to promote dematerialsation of securities. Memers of Depository, also called Depository participants (DP's) can offer demat accounts.
Clearing Corporation like NSCCL carry out the clearing and settlement of trades executed on the equities and derivatives segment of NSE. It aggregates trades over a trading period, nets the positions to determine the liabilities of members and ensures movement of funds and securities to meet respective liabilities. You can see more on NSCCL here.