I use an Arduino MEGA 2560, and I have all the 4 hardware uarts connected to devices. My question is, if I want to connect SD card to the arduino, do I need a uart, or it does not matter?
Most, if not all, SD cards use the SPI bus interface, not serial (UART).
But in order to work with SPI I write
How the program understands that I want to send data over SPI and not on the uart?
You should look at the Reference to see how SPI works. And look at some of the examples that come with the SD library.