The difference between theme and non-theme code is organisational rather than technical. Any code that is active contributes to resulting environment, does not matter where it is loaded from.
There is number of places where code gets loaded from, which are not part of WordPress core:
- active theme (and its parent in for child themes)
- active plugins
- must use plugins
- drop-ins (these are somewhat advanced and serve very specific purposes)
Typical place for your own code, that should not be part of theme, is to create a plugin. Other approaches do not have benefits from generic case, but forfeit interface (managing through admin area) and technical (activation/deactivation/uninstall events) conveniences of normal plugin.