While, in general, television episodes are filmed in the order they are intended to be aired in, there are circumstances in which this is not the case. In these instances, the production codes can be useful in determining the writers' true intentions.
Some "stand-alone" shows, such as The Simpsons or Law & Order, may film episodes in radically different order to how they are aired, because character development and continuity are not major aspects of production. The airing order may in the end be decided by the network, based on ratings, sweeps months, or other network's competition.
Other, more serialized series, like Desperate Housewives, will film all of their episodes in order.
In some rare cases, shows will film episodes out of order to accommodate guest stars' schedules, or to work around main stars' movie schedules. A good example of this is The X-Files, which filmed episodes out of order in its fifth and sixth seasons to accommodate the shooting schedules of main stars Gillian Anderson and David Duchovny.