How do computers deal with RAM latency?
how does a computer know how many clock cycles to wait?


It is hardcoded somewhere. On embedded hardware, it is hardcoded in the firmware. On older computers, it was hardcoded in the BIOS, because all DIMM modules behaved the same. At some point, it became configurable on some computers, in some obscure option menu within the BIOS configuration utility. Now, the DIMM modules contains a small serial ROM called Serial Presence Detect so the BIOS can obtain the latency configuration from the DIMM module itself. So it is now dynamic, but still hardcoded in the DIMM module.


  What does the computer do during the in between clock cycles?


You mean during the DRAM latency clock cycles ? If it absolutely requires the data to proceed, it waits. But processors are now smart enough to make instruction prefetch (they request the opcodes from RAM a few cycles in advance to compensate from latency), and sometimes even data prefetch (when sequentially accessing a big RAM area - but this may require that the developer explicitly request it from his code, using special instructions). Moreover, the level 1 cache between the RAM and the processor most often hides this latency. But, when prefetch failed (for example, because of a conditional jump that was not predicted appropriately) and when data is not available in cache, it has to wait. This does not happen so often, hopefully.


  As well, how does your computer manage the timing if you change the clock speed (overclocking) wouldn't changing the clock speed completely mess up your computer since the RAM only works at a particular speed?


I don't know the details of overclocking much in the PC world, so I'll try not to say wrong things. But I think the RAM and the CPU cores do not have the same clock base. So when you overclock the core, the RAM controller keeps working at his original speed and there is no problem (there are asynchronous interconnects between both, so they can work with independant clocks). Now, I think you can also overclock the RAM. In this case, if you reach a certain limit, you may have to increase the latency to compensate and still have enough time for the RAM to get the bits.