There are a probably a few ways this can be accomplished.
This is the way I have done it in the past.
I created 2 vhosts. the first serves my default site (www) and the other
gets all wild card entries.
From there, you can look at the SUBDOMAIN and do what ever you like with it.
Here is a quick example I came up with.
There is some obvious info missing like logs, error docs, etc.
Code:
<VirtualHost *>
DocumentRoot /var/www/htdocs/default
ServerName your.tld
ServerAlias www.your.tld
</VirtualHost>
<VirtualHost *>
DocumentRoot /var/www/htdocs/dynamic
ServerName *.your.tld
ServerAlias *.your.tld
#this sets an environment variable for use with php
#php example: $_SERVER["SUBDOMAIN"];
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(([a-zA-Z0-9_]+)\.)?your\.tld(:[\d]+)?$ [NC]
RewriteRule .* - [env=SUBDOMAIN:%2]
</VirtualHost>