In circuits like these, this is how the north and south poles of these electromagnets are defined. Now my question is, is the reason that the South pole of the first magnet(Its backside) attracted to the front of the magnet in the back because the two circuits have current flowing in the same direction? Is this why in general the north pole attracts the south pole? How does this work in a permanent magnet such as a bar magnet?
The magnetic attraction/repulsion is a fundamental property of magnetism. N-poles attracts S-poles and so on, and this is based on our observations of how nature works.
Now, there is a strong relationship between electricity and magnetism, which explains why a flowing electrical current produces magnetic fields. Once the magnetic field is produced, the S-N attraction can be observed. And it is also true that currents flowing in the same direction will attract each other, and repulse each other when flowing in opposite directions, through the interactions between their mutual electromagnetic fields.
On permanent magnets, the source of the magnetic fields has to do with the individual properties of atoms in the magnets, and how they are aligned into a preferential direction. Again, once there is a net alignment of atoms in a particular direction, there is a resulting magnetic field at the macroscopic level, which will then follow the rules of N-S attraction.