Yes this should be possible with Discourse Automation — the plugin includes an automation called “add user to group through custom field”
So at signup you could require a custom field with the options “student” and “teacher”, and based on what a user selects they can be automatically added to the relevant user group. The user group can then be given access to specific categories (more info on this in Understanding groups and category permissions (security settings))