Voltage is not an absolute number. When we say "this is 24v" what we are actually saying is that the positive terminal is +24 compared with the negative terminal. That's why when you mix components with different voltage needs, as in your case, you have to connect the GNDs together, so that the reference is the same for the whole system.