How should I fasten window trim molding when I reinstall it?
First off, don't use wood trim. Vinyl trim is easy to find and it won't rot.

Second, you can use an air nailer if you can avoid the window. Caulk will cover the holes. I would suggest using galvanized nails. If you don't feel comfortable, or you don't own an air nailer, the next best option is construction adhesive. If you went vinyl trim MAKE SURE THE ADHESIVE SUPPORTS VINYL. If you don't, it may eat the trim instead. Make sure it's outdoor as well. The catch with adhesive is it will make future repairs harder.

Third, get a good outdoor grade caulk. It will prevent water intrusion