When is software protected by copyright
Copyright protects almost any creative work, including software, if there was creative activity involved and the work was developed independently. Although the required level of creativity is low, (parts of) a program may still be unprotected. An implementation can be purely functional, or be so restricted by external factors that there is no room for creativity left.
Software is protected by copyright if its development involved creative activity. The required level of creativity is very low. When writing software, a programmer has to make certain choices: texts for dialog boxes and menus, whether to use a series of if-statements or a switch-statement, and so on. These choices indicate the work was the result of creative activity.
Idea versus expression
The creative activity should be the expression of an idea. Ideas are not protected, but only the particular expression of that idea. For example the idea to use the right mouse button for context-sensitive menus is not protected. It is a mere idea that anyone may use. Still, someone's code implementing this idea is protected and may not be copied without permission.
In some cases the idea and its expression can be very close. In particular, if there is only one way to express the idea in software, that one implementation is not protected. This is rare; even with trivial programs like "Hello, world" a programmer can usually choose equivalent instructions to print the text, or to start and stop the program.
An implementation can be purely functional, that is, be the result of a straightforward translation of the requirements into code. A program that only displays a Windows dialog box with "Hello world" can be written without any creativity. That program would not be protected.
This does not only apply to trivial programs. An implementation of a standard algorithm may require very little creativity and if so, is likely not protected by copyright. The function of a work can only be protected by a patent.
Even when providing a largely functional implementation, creativity can still be found in things like function and variable names, code optimizations and even the comments accompanying the code. Copying such an implementation wholesale then still infringes on copyright.
Limitations because of external factors
If an implementation must be done in a particular way, there is no room for creativity, which means the implementation is not protected by copyright.
A program could be the result of simply putting together straightforward calls to one or more given APIs (Application Programming Interfaces). Such a program would not be protected.
But in many cases the programmer still has some room for personal choices, which results in the required creativity. There could be equivalent functions in an API, for instance. And functionality beyond the API calls is not dictated by the API structure, so that is likely to be creative.
The second main requirement is that the program is developed independently. Independence does not mean that nothing similar may exist. It is very well possible to independently create a program that has similarities with an existing program. For example, two programmers implementing the same algorithm may end up with very similar code. Both their implementations are protected by copyright and neither infringes on the copyright of the other.
Of course, if there are many similarities, the author of the older work may allege the author of the younger work has copied his work. But if the latter author can show (for example, with early versions, flowcharts and so on) that he independently developed his work, there is no infringement of the copyright on the older work. Good version control management can be very important in proving independent creation of a computer program.