I don't know much either but here's my understanding:

Transistors at that scale aren't placed individually - masks are used on different layers of material directly and it's a chemical process

> I would assume a small percentage of them don't work

Designs are built with tolerances so that small printing differences shouldn't make bad connections and break the chip, and then chips are rigerously tested so that sections with bad connections are either turned off (eg storage devices can be sold as lower capacity if one section is failing), or the chip is unusable

> How are machine code written for these? Do they track and control the values of individual transistors? And, how is then the assembly language written to translate this to machine code?

Machine code doesn't know anything about transistors - the transistors are combined to build a very complicated logic gate. Machine code are inputs to that logic gate that tell it how to control the output

A transistor is a logic gate, either a 'nand' or a 'nor' based on the type (kinda but it's a bit different because it controls if one wire can connect through). You can combine lots of logic gates to make for example an adder. Give it two binary numbers as input and it produces an output.

CPUs are very complicated logic gates, but they're just logic gates. They take input as a bunch of 1s and 0s and produce output. Here's an example of how a simple CPU could work:

As input, it takes:

- some register values. for example, a program counter and three registers r1 r2 r3

- a value that was read from memory

As output, it emits:

- new register values. These are routed back in to the input so they can be used next cycle

- an address to read memory from

Looking at a simple program:

- load 100, r1
- load 200, r2
- add r1, r2, r3

here is how this cpu would execute it:

The program is stored in memory. Let's say it's at 0x00

The cpu starts by reading address 0x00

Next cycle, data is returned from the memory. An instruction 'load 100, r1'

The cpu is a big logic gate - it takes this input:
- program counter: 0x00, r1: 0, r2: 0, r3: 0
- memory value: 'load 100, r1'

And it produces output:

- program counter: 0x01, r1: 100, r2: 0, r3: 0
- read memory at: 0x01

Internally, using logic gates it determined that the instruction was 'load' and it outputted the value into the right register, all with a big set of logic gates. It also increased the program counter to know what instruction to read next, and outputted 0x01 as a read address to get the next instruction from memory

The next cycle, it gets this input.

- pc: 0x01, r1: 100, r2: 0, r3: 0
- memory value: 'load 200, r2'

And produces output:

- pc: 0x02, r1: 100, r2: 200, r3: 0
- read memory at: 0x02

The cpu is just a logic gate, so it has no retained knowledge, it only produces output based o its input. Real cpus can have internal state because latches can be made out of transistors too.

Next cycle:

- pc: 0x02, r1: 100, r2: 200, r3: 0
- memory value: 'add r1, r2, r3'

And produces output:

- pc: 0x03, r1: 100, r2: 200, r3: 300
- read memory at: 0x03

Real CPUs are much more complicated and keep lots of internal state and often even run their own internal machine code that they use to translate your code into stuff that's easier for it to understand

I hope this helps as a starting point to know what to look into