where do you handle the POST requests? where do you write the validation rules and flash error messages? where do you write save to db code? (mainly i want to know how do you route a POST request to a controller and where do you write the controller?)
You'd do this wiht a Craft plugin or module.
Try giving the plugin docs a once over: https://docs.craftcms.com/v3/extend and if you have some specific questions, feel free to post back.
You can also generate Craft plugin boilerplate code at https://pluginfactory.io that already has a lot of this setup for you.