How do you switch and iphone from tmobile to at&t free of charges?

My friend broke my phone, so he gave me his iphone 7 plus, but it is on tmobile, is it possible to change carriers from tmobile to at&t free of charges?

Yes. You need to get T-Mobile to unlock the phone. Though you will need your friend's account PIN to initiate the unlock request. If your friend doesn't want to give you his account PIN (you can do a lot of nefarious things with it, so I wouldn't blame him) he will need to initiate the unlock request himself.

Once the phone is unlocked, you can put in your AT&T SIM and it should accept it and work with no problems.

Here is the official T-Mobile Unlock information page

No. First you have to unlock it from TMobile. Unless the phone qualifies for free unlocking it'll cost you. To qualify you usually have to be a paying customer for a certain amount of time. All this varies from one provider to the next. Once it's unlocked ATT will charge you an activation fee.