Why is a urine test performed during pilot medical examinations?
It's for diabetes and kidney disease. You can read the AME guidance here:


  Item 57. Urine Test
  Glycosuria or proteinuria is cause for deferral of medical
  certificate issuance until additional studies determine the status of
  the endocrine and/or urinary systems. If the glycosuria has been
  determined not to be due to carbohydrate intolerance, the Examiner may
  issue the certificate. Trace or 1+ proteinuria in the absence of a
  history of renal disease is not cause for denial.


Glycosuria is an indication of diabetes, and proteinuria is an indication of kidney disease (among dozens of other things, if Wikipedia is to be believed). And the AME can do other tests too, if he thinks they're necessary:


  The Examiner may request additional urinary tests when they are
  indicated by history or examination.


The regulatory reference (per the AME guide) is 14 CFR 67.113 for a first-class medical, but exactly the same regulation is there for second- and third-class too.