Usually you can define a separate “DocumentRoot” for each subdomain (see your hosting provider cpanel) and than place the same code in each documentroot directory with the only difference being a database name these copies are using
You can set up a wildcard * DNS entry with your domain registrar or web host (wherever your DNS is hosted) and you will probably have to ask your web host to make the necessary configuration changes. I think you will have to use mod_rewrite to rewrite the subdomain in the form of ri.site.com -> site.com/file.php?state=ri.
Did you try searching here at Sitepoint? I know this question has been asked many times before.
As for setting up separate databases for each state, there is little good reason for doing this. Managing fifty databases is going to be much more difficult than managing one.
An easier alternative would be to use mod_rewrite and the state name after the slash like this: site.com/ri or site.com/rhodeisland.
I do not know if this is any longer true, but I believe Google used to view each subdomain as a separate site. If so, this will not be beneficial to you. Check this out for more information:
Chances are, all your sites will have more or less the same content, so google will think they are different sites, and only rank one of them (it will think the rest are copies).