Why do some ATMs charge you to take out your money?

Because they can.

(Seriously. The bank who owns them sees a chance to make a profit, so naturally they're going to take it!)

If you stick with only ATMs in your own bank's network, they won't charge you those fees.

These ATMs are owned by third parties; not your bank. They are sort of like real estate agents who don't own the house they're selling, but get a commission on the sale.

If you use an ATM owned by your bank, there shouldn't be any type of charge.

Also to cover operation cost.

So they can make money off of your transaction.