Try using Moore-Penrose pseudoinverse $A^+\\!$, it coincides with $(A^TA)^{-1}A^T$ when the inverse exists but is defined for any $A$. When $A^TA$ is singular there are multiple least square fits, and pseudoinverse will give you the one with the smallest Euclidean norm. Stable numerical methods for computing $A^+$ are based on singular value decomposition or the QR method, but if you are using MATLAB or something similar there should be a direct command for it.