Transistors are required for the Teensy 4.1 to driver the opto-couplers on your stepper motor driver, for two (2) reasons.
1) If your optocoupler is connected to V > 3.3V, the Teensy will be damaged. That means your Teensy will need to be replaced. This means, if ENA+, PUL+, or DIR+ are connected to +5V or higher and you directly connect to ENA-, PUL-, or DIR- with a Teensy4.1 output, it is likely your Teensy will be damaged. Your Teensy, might by chance survive, but it will not be for long.
2) The Teensy output can only drive 4mA, which is not enough to drive the opto-coupler, according to the stepper motor driver specifications. The stepper driver will not respond reliably, or may not work at all.
If you use a transistor buffer, as I previously showed, then the Teensy will only need 1mA, which the Teensy can easily supply. The transistor will amplify the current, by it's beta (gain). The transistor, as I showed, will easily be able to sink the required current needed to turn on the opto-coupler. To learn about transistors, there are thousands of sources online, and just as many books on the subject. For the simplest introduction, read the Wikipedia article titled Transistor.
A pair of resistors to drop the voltage could work, but is a poor solution many times. It will not work at all to drive the opto-coupler. Why? Because the Teensy cannot supply more than 4mA, either sourcing or sinking. Adding resistors does not change this fact. It could even make things worse. Resistors are passive devices, they cannot amplify current. Transistors are active devices, they CAN AMPLIFY current. To drive your opto-coupler you need an active device between the Teensy and the opto-coupler. A transistor is a cheap solution. You could use an appropriate (active) buffer or driver, as well.
For digital inputs, sometimes using a resistive divider can work, for slow speed inputs. If you want to reliably drive a Teensy input, the voltage MUST be between 0V and 3.3V at all times. Resistive dividers can slow down or retard rise times of signals, depending on your load capacitance. There is a whole thread on voltage translation - in the Technical Support Forum, I suggest you read it. There is a lot of information there, and you can learn about the pluses and minuses of different approaches. Personally, I have used a 74LVT245 device for a buffered input to the Teensy. I run my encoders through the 245 device. The 74LVT245 can drive +/-24mA. The inputs are +5V tolerant, even though Vcc=+3.3V. This means, the 74LVT245 can receive a 0-5V signal and translate it to a 0-3.3V signal. I am sure there are other devices, perhaps even better, cheaper or faster. A 74LVT245 device works fine for my application, using a spindle rotary encoder and DRO read heads.
This advice is being given to avoid you some heartache. If you connect your Teensy 4.1 to 5V devices directly, your Teensy will be damaged and may fail completely. Please read the Teensy 4.1 product page word for word. There is very good, valuable and essential information on that page. It's how I started to learn about Teensy's. Good luck with your endeavor.