Assalam aleikum,
Allah subhanahu wa ta'alaa has provided us through Qu'ran and sunnah with incredibly comprehensive guidance about how we must live our lives, alhumdulillah. He has set out clear limits in the form of the shariah about how we must behave in order to ensure a peaceful and just society where families can live in safety alhumdulillah. The shariah is undoubtedly the most perfect legal system because it is divine. Our Creator has wisdom beyond our comprehension and knows what is best for us - it is not for us to question. Like every legal system (even the most useless man-made system), it includes a system of punishments for stepping over the mark. It has been said that some of the punishments used in Islam are out of date and harsh, but they have been proven over 1400+ years to be very effective deterrents and help to provide the safe and secure society that we all want when they are used as part of a fully Islamic system.
So to answer to the OP's question, punishments are given in Islam because Allah subhanahu wa ta'alaa has commanded us to do so in certain cases, to help protect the fabric of society and deter others who may be thinking of sinning in the same way. We may also pray for the person that has done wrong, but the punishment itself is a key part of the juducial process, which is why in Islam they are often carried out before witnesses.