Because there isn't a definite article here (word for the). You are correct that an sgoil on its own would be "the school". However, in old Gaelic an used to also mean "in". The word ann also means in.
So historically "in a school" would have just been "an sgoil". However, that also would have meant "the school". So to save confusion people started saying "ann an" (literally in in") as the word in.
So ann an is the single word used for "in". Ann an sgoil means in a school.
If you were to say in THE school, you would add an S to ann. So in the school would be anns an sgoil.