Programmatically add a taxonomy term to an existing content type
You should check out the Views Bulk Operations (VBO) module. It allows you to create a View to generate a list of target nodes (or other things like Users) and perform an action on each of those nodes. One fo the available actions is modifying the taxonomy terms used on these nodes. This is actually the exact example used in the documentation for VBO on d.o. It seems like this is exactly what you're looking to do, just not actually programmatically. Maybe that's ok... but if not, you can create this VBO and then execute it programmatically using views_bulk_operations_execute.