Why do endings of nouns change in Polish?

in the sentences "chlopiec na stole" (boy on bench) i "chlopiec pod stolem"(boy under bench) , why does the word for bench (stolem) change form? and instead of "dziewczynka i kon" why do you say "dziewczyna i kon"?
i love learning polish, but those rules confuse me!
( sorry i dont have the letter for "w" on my keyboard!)
"dziewczynka i koń" "dziewczyna i koń"?

sorry i spelled horse wrong!

Sorry for not having the accent mark over the S in the way it's spelled because there is no option on my keyboard to do so just look at the word in the way I have it.