Where should I put this JavaScript code ... the Drupal way?
Why don't you simply use the Maxlength module that does exactly what you want already pre-packaged?

Otherwise, I would either write a custom module and use drupal_add_js() and hook_form_alter(), or use them in your template.php file without having to write a module.

If you're stumped, and want to know how to do it, why don't you download the Maxlength module and analyse how it is written to clarify the processes involved?