@HighFunctioningSunflower I assume when you ask "how it works" you mean "how the different character shapes are selected"? The keyboard just enters letters as in any other language, but it's up to the font rendering engine to decide the appropriate glyph to represent each character based on context. For example, here's the Persian letter H, and a small line (kas͟hída) that represents any letter to which the H might be connected.
The initial form (هـ) is used when there is a letter to the left, but not to the right.
The medial form (ـهـ) is used when there are letters to the left and right.
The final form (ـه) is used when there is a letter to the right but not the left.
The isolated form (ه) is used when there are no adjacent letters.
In all of those lines, I typed the same character on the keyboard for H, and the context (i.e. position of the adjoining line) tells your device which shape to use. The Latin alphabet equivalent would be typing (H_), (_H_), (_H), & (H) and seeing the letter H assume a different shape in each instance, even though you typed it in with the same key all four times.
@HighFunctioningSunflower H is just the letter I chose because its Persian equivalent ه has four very distinct shapes, so it's easy to see how it changes based on context. Since Persian is written right-to-left, the initial letter of a word appears on the right, while the initial letter of a Latin-alphabet word appears on the left. But in both cases you type the word first-to-last and the computer arranges the letters left-to-right or right-to-left as appropriate for the script you're using.