All four describe situations involving potential harm, but they differ in tone and scope. 'Dangerously' is the most common and general word — it covers physical injury, damage to objects, and metaphorical risk (e.g. dangerously close to bankruptcy). 'Perilously' carries a stronger, more dramatic tone and is common in formal writing or storytelling (e.g. the climber hung perilously from the cliff). 'Precariously' focuses on unstable positions that could collapse or fall (e.g. the stack of boxes sat precariously on the top shelf). 'Riskily' highlights deliberate decision-making rather than accidental hazard (e.g. they invested riskily in a start-up). When the situation is an immediate physical threat, 'dangerously' is the natural first choice in everyday English.
From Old French 'dangier' (power, danger), from Latin 'dominus' (master, lord). The sense shifted from 'power/jurisdiction' to 'risk' during the medieval period. The suffix '-ous' (full of) was added to form 'dangerous', and '-ly' turns the adjective into this adverb.