You have the endstop which sends a on or off to the controller board.
The boards firmware knows if it is a close or open switch.
Lets assume pressed is on and unpressed is off.
When the axis is at home it will read as ON. The firmware then will not allow the axis to move in the direction you have designated is the Home direction. Now this is probably where you got confused. You in fact are the one to open a code file, and say what direction is home. When your turn it on the first time, chances are you have done it wrong on at least one. Then you will frantically go and manually press it yourself as your printers hotend crashes HARD into the bed.. All of us have been there !
Generally Prusas use ramps 1.4
http://reprap.org/wiki/RAMPS_1.4
Your code is your firmware. Usually you just edit a header file (single file). These files are heavily documented, depending which you use. Your seller might provider you with a preconfigured header file.
Will note that yes, if the endstop slips or is not position right, the printer will crash into it self. Usually no damage, but it is a very unpleasant thing to happen.
Also worth noting that some firmware will force you to home before you move. Others will let you crash the printer on the opposite side. Just a motor, will do whatever you tell it to.
In short. You tell it how the endstops work and what direction is home on a printer.