Magnetic lines of force in opposite direction repels and in same direction attracts.
When two magnets are placed with their like poles facing each other, the lines of force are in opposite directions and hence like poles repel each other. When the unlike poles of a bar magnet face each other the magnetic lines of force are in the same direction and hence unlike poles attract each other.