A/ Count the number of existing cassette sprockets
B/ Identify the gears system brand and the model (derailleurs and shifters)
C/ Check the freewheel body length with the gears number of the cassette (compatibility)
D/ Check the gears number of the chain, shifters and rear derailleur (the chains 9, 10, 11 and 12s have not the same width even if it is the same brand)
E/ Identify the capacity of the rear derailleur. For example, a rear derailleur Shimano Deore RD-M615-SGS 10 speed can accept a sprocket with 32 teeth maximum and 11 teeth minimum, for a capacity of 43 teeth.
Take care the Shimano and Campagnolo groupsets are not compatibles, indeed the sprockets and washers have not the same tickness. But a Sram cassette will be compatible with Shimano groupset.
If you want to replace a used cassette by a new one, you will have to replace the chain also, because after thousand kilometers the chain stretches. With a used cassette, you have to use a used chain.
For a cassette replacement by a new one with smaller sprockets you will have to reduce the length of the chain. If the sprockets are higher, the chain length will be longer.
It will be possible to use a "road" cassette on a mtb and conversely if the derailleur capacity can do it.