What caused my truck to randomly start smoking/burning near the headlight housing?

My truck randomly started smoking when i turned on my headlights, so i pulled over and popped the hood and a "burnt plastic" smelling smoke was coming from behind the headlight, i turned everything off and took out my keys but it kept smoking and was about to catch fire, it was alot of smoke, so i yanked off the battery cable and it stopped. Luckily i saved my truck from a blaze but i want to fix the problem myself if possible, the truck is a 2000 ford ranger, regular lights (no daytime auto lights) but the lights won't turn off at all, they are very dim and kind of flicker, and the highbeam dashlight symbol is also stuck on, any ideas?

Imagine that… A ford with a SHORT CIRCUIT.

Well, I guess I would start by looking at the wires that go to that head light. Look for melted insulation and any signs of the wire rubbing on metal.

It wasn't random, it was when you turned on the headlights. I'm wondering why you didn't turn them off instead of pulling the battery cable. I'm also wondering why you don't simply look at the area that was about to catch fire. Surely it will be easy to see where the wiring failed and what needs to be done to fix it.
Much easier in fact than asking people on the internet that can't look at it themselves.

You have a shorted wire… Locate it and have it replaced.

Check headlight relay, main wires from fusebox to headlights. Headlights wired from battery to relay to headlights maybe 12 gauge wires. Relay has secondary smaller wires to headlight switch. Turn off ignition and turn off, push in headlight switch takes that switch out of circuit but relay is still in circuit, battery cable pulled stops burning= likely relay or relay wiring to check first. Look for rubbing on something close to headlight, bared wire or a bit of some metal right at connection, maybe some trash. Test light from battery cable to battery post on ground or positive will give strong hint of closed/short circuit. Headlight switch failure usually means a fuse will blow, relay failure usually means no lights but short in wiring from relay to headlights or sometimes in relay from the other short 'locking' relay closed would give power to headlights and the burning until circuit broken as you did with pulling cable.