There is a really nice library for working with encoders on the Arduino platform as well as on Teensys. It's developed by PJRC, the developer of the teensy, and has a very nice explanation on its page which can be found here: https://www.pjrc.com/teensy/td_libs_Encoder.html
To answer your question a bit more comprehensively, encoders work, by encoding the rotation into so-called grey-code.
Grey-code is a form of electrical signaling where you have two pulse trains, that look like this:

This method of signaling allows a microcontroller like an Arduino to sense both the direction of rotation, as well as amount of rotation.
One way to read gray code is to trigger when one of the pins, say pin1, goes high, and then read the state of the second pin. Due to the nature of grey-code, if you are rotating one direction, pin 2 will always be low when pin 1 goes high one direction of rotation, while it will always be high for the opposite direction of rotation.
A good way to implement this is to use interrupts to detect when a pin switches state because encoders will generate signals very fast with moderate rotation speeds because a typical encoder used with an Arduino has 4 electrical steps per physical indent. However, you will still have several problems to overcome. First of all, being a mechanical switch, you will have to deal with switch bounce, but amongst other things to deal with is code execution speed, to ensure that you don't miss any impulses on faster moves.
All in all, my recommendation would be to use the library provided by PJRC, as it is a very well performing library, and supports multiple modes of operation, with varying levels of performance.
General tips for the highest performance of reading encoders is to use interrupts as much as possible, to ensure impulses are caught.