The use of 4.7kOhm resistor with DS18B20 temperature sensor
The product page FAQ [https://www.maximintegrated.com/en/support/faqs/ds18b20-faq.html] has an entry:
Q: Is the 4.7kΩ resistor needed for 5V and 3.3V operation?
A: Yes, the 4.7kΩ pullup resistor is required for both 5V and 3.3V applications.
The 1-wire bus requires that the control signal be pulled high so the master device can pull it low to ask for data, and the slave device can pull it low to give the data. This allows you to have multiple 1-wire devices on the same "one wire".
The data sheet notes another purpose:
Another feature of the DS18B20 is the ability to operate
without an external power supply. Power is instead
supplied through the 1-Wire pullup resistor through the
DQ pin when the bus is high. The high bus signal also
charges an internal capacitor (CPP), which then supplies
power to the device when the bus is low. This method of
deriving power from the 1-Wire bus is referred to as “parasite
power.” As an alternative, the DS18B20 may also be
powered by an external supply on VDD.
Adding to jose's answer above to answer your other two questions
And why this resistance? When the device doesnt have its own power supply it uses the power going through the pull up resistor-thus with this resistance it is powered properly. Also the other reason- as Jose wrote is because it the value which differentiates high or low bits.
What happens if we don't put a resistor at all or use a different resistance? If you are going to you an out of spec value-dont expect the device to work properly because its using the wrong resistance-e.g. it may not work because if the resistance is too high-then according to ohms law it may not receive enough parasitic power. Though it should be able to tolerate the error tolerance of the resistor-so changing it by a few ohms either way from 4700 ohms will not make a difference. You may damage the device without a resistor.