What are the main goals of administering preliminary / qualifying exams to graduate students? If I were a Spanish Major as an undergraduate and decide to pursue a PhD in a completely unrelated field (like Theoretical Physics), it makes sense to give a qualifying exam to check that I had the necessary skills to begin the program.  But if I'm coming from a B.S in math to a PhD program also in math, it doesn't seem to make sense to give a qualifying exam, as if the knowledge I gained in my undergraduate was insufficient.  I presume that one is accepted into a PhD program because he/she has already demonstrated the "qualifying" skills.  Thus, I'm baffled by the notion of the qualifying / prelim exam.   I'm curious about the ultimate goals of these exams, and how they relate to the professional development of a graduate student.
There are two types of qualifying/preliminary exams, those that cover graduate or undergraduate topics.  I assume you are just talking about the latter (while in my experience the former is a little more common).

As I see it, the big reason for preliminary exams on undergraduate material is that many students haven't actually mastered this material to a professional standard.  At less prestigious schools, many students won't have received straight A's (or the equivalent in other grading systems), so they had gaps or weaknesses in their understanding, and unless they took more advanced courses in the same area they may never have filled those gaps.  Furthermore, it's possible to get excellent grades without true mastery, and students sometimes forget things they knew while taking a course.

The preliminary exam sends a message of "OK, you're in grad school now, and as a professional you're expected to know this material cold.  If you have any doubts about your mastery of it, now is the time to study carefully."  The few students who don't need to study suffer little harm from the exams, and the students who do need to study benefit from the studying.