The word 'legal' entered English in the early 16th century, either from Old French 'légal' or directly from Latin 'lēgālis' (pertaining to the law), formed from 'lēx' (genitive 'lēgis,' meaning law, statute, rule) with the adjectival suffix '-ālis.' The Latin noun 'lēx' is generally traced to PIE *leǵ- (to collect, to gather), reflecting the idea that a law is a 'collection' of rules or a 'reading' of regulations — since the related Latin verb 'legere' means both 'to gather' and 'to read' (one 'gathers' meaning from written words).
The PIE root *leǵ- is remarkably productive across the Indo-European languages. Through Latin 'lēx,' it gave English 'legal,' 'legislate' (from 'lēgis lātor,' proposer of a law), 'legislature,' 'legitimate' (from 'lēgitimus,' lawful), 'privilege' (from 'prīvilēgium,' a law applying to an individual — 'prīvus,' private, + 'lēx'), 'legacy' (from 'lēgātum,' a commission or bequest), 'delegate' (from 'dēlēgāre,' to send with a commission), 'allege' (from 'allēgāre,' to cite), and 'illegal.'
Through the alternate form 'legere' (to read, to gather, to choose), the same root produced 'lecture' (a reading), 'lesson' (from 'lectiō,' a reading), 'legend' (from 'legenda,' things to be read), 'legible,' 'elect' (from 'ēligere,' to choose out), 'select' (from 'sēligere,' to choose apart), 'collect' (from 'colligere,' to gather together), 'intellect' (from 'intelligere,' to perceive, literally 'to choose between'), 'neglect' (from 'neglegere,' to not gather, to disregard), and 'elegant' (from 'ēlegāns,' choosing carefully, tasteful).
Through Greek 'légein' (to say, to gather, to reckon) and 'lógos' (word, reason, account), the root produced 'logic,' 'logarithm,' '-logy' (the study of), 'dialogue,' 'catalogue,' 'prologue,' 'epilogue,' 'analogy,' 'apology' (from 'apologíā,' a speech in defense), and 'lexicon' (from 'lexikón,' of or for words). The connection between 'legal' and 'logic' thus runs deep — both derive from a root meaning 'to gather, to collect,' reflecting the idea that both law and reason involve the systematic gathering and ordering of rules and arguments.
English borrowed from this root at multiple times and through multiple paths, creating doublets: 'legal' and 'loyal' are the same word, the first keeping the Latin form and the second reshaped by Old French phonology ('lēgālis' → Old French 'loial' → English 'loyal'). A loyal person was originally one who was lawful, who kept to the law. Similarly, 'regal' (kingly, from 'rēx') and 'royal' (from Old French 'roial') are doublets from a different Latin word, following the same sound-change pattern.
The distinction between 'legal' and 'lawful' in English is subtle but real: 'legal' tends to describe what is established by or pertaining to the formal system of law (legal proceedings, legal tender, legal counsel), while 'lawful' often carries a moral or natural-law connotation (a lawful act is one that is rightful). 'Legal' is the Latinate term; 'lawful' is the Germanic one — from Old English 'lagu' (law), borrowed from Old Norse 'lǫg' (law, literally 'things laid down').