Magnet have 2 poles south pole and north pole, when same poles of two magnets are brought together they will repel each other.when different poles are brought together they will attract each other.The magnet will also attract to iron
This attraction and repulsion is due to the magnetic field.The magnetic field is a large topic.AAbout this you will learn in your higher classes