How can I have dmg files automatically unmount when I trash them?
I found two options


Use a folder action on ~/.Trash to unmount any DMG getting moved into the trash can (see second part of this article, summarized below)
DMG Cleaner claims to be able to do this as well




Folder Action to unmount DMG moved to Trash


Create a new "Folder Action" workflow in Automator
Set Folder Action to work on ~/.Trash
Add a "Run Shell Script" action to the workflow 
Set shell to "/usr/bin/python" and make sure input gets passed as arguments
Replace the content of the script box with the following script

import string, os, sys
lines = os.popen("hdiutil info").readlines()
should_eject = False
for line in lines:
    if line.startswith("image-alias"):
        path = line.split(":")[1]
        image_path = path.lstrip().rstrip()
        if image_path in sys.argv:
            should_eject = True
    elif line.startswith("/dev/") and should_eject is True:
        os.popen("hdiutil eject %s" % line.split()[0])
        should_eject = False
    elif line.startswith("###"):
        should_eject = False

Save the workflow