PCA is applied to reduce a dataset into fewer dimensions while retaining most of the variation in the data. It works by calculating the covariance matrix of the data and extracting eigenvectors with the highest eigenvalues, which become the principal components. The EJML Java library can be used to perform PCA by adding sample data, computing the basis using eigenvectors, and projecting samples into the reduced eigenvector space. PCA is generally not useful for datasets containing mostly 0s and 1s, as such sparse data is already in a compact format.