The fee you pay is based on the duration of your visa rather than the duration of your course. The surcharge will be a fee of £150 per year for each year you are in the UK. Where your stay in the UK includes part of a year they will charge you £75 for periods of 1-6 months and £150 for periods of 6-12 months. For example if you are doing a 12 month Masters course they will charge you £225. This is because your visa will be issued for a total of 16-17 months. You can find out how much will be expected to pay using the fee calculator on the UKVI website.
You will be required to pay the full fee for the entire duration of your stay in advance. The payment is made as part of your visa application, and your visa will not be granted if you have not paid the fee or if you have paid the incorrect amount.
You can find more information about the health surcharge on the UKVI website.