Here is a link that may help explain this to you,

https://en.wikipedia.org/wiki/Galvanic_cell